我想在64位程序集中編寫一個函數(最大值),我不知道我在做什麼錯誤,也許你們有些人可以確定我做錯了什麼:/在64位程序集中編寫函數
這裏的功能:
int max(int a, int b) {
/* Return the larger of the two integers `a' and `b'. */
if (a > b)
return a;
else
return b;
}
這裏是我的彙編代碼(帶註釋):
push %rbp
mov %rsp, %rbp
mov %rdi, %rax
mov %rsi, %rcx
test %rax, %rax // Checking if first parameter is signed
js .signedRAX
test %rcx, %rcx // Checking if second parameter is signed
js .signedRCX
jmp .compare // If either one signed then jump to .compare
.signedRAX:
test %rcx, %rcx // Checking if both are signed
js .signedRAXandRCX
mov %rcx, %rax // If not then return the positive number
jmp .end // finish the function
.signedRCX:
jmp .end // If only the second parameter is signed then jump
.signedRAXandRCX: // straight to end of function and return %rax
cmp %rax, %rcx // If both are signed compare which one is the max
jl .end
mov %rcx, %rax
jmp .end
.compare:
cmp %rax, %rcx // If both are positive then compare which one is
jg .end // the max
mov %rcx, %rax
.end:
mov %rbp, %rsp
pop %rbp
ret
比較兩個參數都登錄,然後都當我得到了錯誤的輸出正。
只是'子a,b'和測試結果使用'jg','jge','jl'。就這樣。 – alexander 2014-10-31 10:09:41
所以我可以跳過所有的跳轉來檢查簽名? – drleifz 2014-10-31 10:11:12
@drleifz這正是'jg' /'jl'測試和'ja' /'jb'測試之間的區別:後者對無符號數有效。 – glglgl 2014-10-31 10:14:47