本帖最后由 侃遍天下无二人 于 2023-3-29 19:47 编辑
这种咋实现要是C标准中没规定那就和具体的编译器有关了,你弄成反汇编看看,一会我上传我这边的结果
[Asm] 纯文本查看 复制代码 .text:0000000000401550 ; int __cdecl main(int argc, const char **argv, const char **envp)
.text:0000000000401550 public main
.text:0000000000401550 main proc near ; CODE XREF: __tmainCRTStartup+242↑p
.text:0000000000401550 ; DATA XREF: .pdata:000000000040506C↓o
.text:0000000000401550
.text:0000000000401550 c= byte ptr -9
.text:0000000000401550 b= dword ptr -8
.text:0000000000401550 a= dword ptr -4
.text:0000000000401550 argc= dword ptr 10h
.text:0000000000401550 argv= qword ptr 18h
.text:0000000000401550
.text:0000000000401550 55 push rbp
.text:0000000000401551 48 89 E5 mov rbp, rsp
.text:0000000000401554 48 83 EC 30 sub rsp, 30h
.text:0000000000401558 89 4D 10 mov [rbp+argc], ecx
.text:000000000040155B 48 89 55 18 mov [rbp+argv], rdx
.text:000000000040155F E8 EC 00 00 00 call __main
.text:000000000040155F
.text:0000000000401564 C7 45 FC 06 00 00 00 mov [rbp+a], 6
.text:000000000040156B C7 45 F8 EC FF FF FF mov [rbp+b], 0FFFFFFECh
.text:0000000000401572 8B 55 F8 mov edx, [rbp+b]
.text:0000000000401575 8B 45 FC mov eax, [rbp+a]
.text:0000000000401578 01 D0 add eax, edx
.text:000000000040157A 83 F8 06 cmp eax, 6
.text:000000000040157D 76 06 jbe short loc_401585
.text:000000000040157D
.text:000000000040157F C6 45 F7 01 mov [rbp+c], 1
.text:0000000000401583 EB 04 jmp short loc_401589
.text:0000000000401583
.text:0000000000401585 ; ---------------------------------------------------------------------------
.text:0000000000401585
.text:0000000000401585 loc_401585: ; CODE XREF: main+2D↑j
.text:0000000000401585 C6 45 F7 00 mov [rbp+c], 0
.text:0000000000401585
.text:0000000000401589
.text:0000000000401589 loc_401589: ; CODE XREF: main+33↑j
.text:0000000000401589 B8 00 00 00 00 mov eax, 0
.text:000000000040158E 48 83 C4 30 add rsp, 30h
.text:0000000000401592 5D pop rbp
.text:0000000000401593 C3 retn
.text:0000000000401593
.text:0000000000401593 main endp
重点关注这几行:
[Asm] 纯文本查看 复制代码 .text:0000000000401564 C7 45 FC 06 00 00 00 mov [rbp+a], 6
.text:000000000040156B C7 45 F8 EC FF FF FF mov [rbp+b], 0FFFFFFECh
.text:0000000000401572 8B 55 F8 mov edx, [rbp+b]
.text:0000000000401575 8B 45 FC mov eax, [rbp+a]
.text:0000000000401578 01 D0 add eax, edx
.text:000000000040157A 83 F8 06 cmp eax, 6
其中在 add eax, edx之前,变量只在不同寄存器之间传递过,没有做任何处理,所以相加的时候也是两个数做补码运算,结果是 6-20=-14 的补码,自然不可能大于6了
另外无符号的意思不是把负号去掉,是把原有的符号位视为数 |