问题
I'm currently writing a NASM program on x86 Linux and I'm trying to perform a calculation that divides the first command line arg (a year) by the first leap year check. I want to check if the remainder is 0 or not but I'm struggling with how to do that. I know the div command stores the answer in a certain register and a remainder in another but right now I'm just using test. Here's the code
global main
extern puts
extern printf
extern atoi
section .text
main:
sub rsp, 8
cmp rdi, 2
jne error1 ; jump if aguments != 1
mov rdi, [rsi+8]
call atoi
test rdi, fourTest
jnz notLeapYear
jmp done
testTwo:
jmp done
notLeapYear:
mov edi, nLeap
call puts
jmp done
error1:
mov edi, badArgs
call puts
jmp done
done:
add rsp, 8
ret
badArgs:
db "Requires exactly one argument", 5, 0
nLeap:
db "Not a leap year", 5, 0
section .data
fourTest: dq 4
hundTest: dq 100
fHundTest: dq 400
I believe I need to change the test rdi, fourTest to using div but don't know how to isolate the remainder and determine if I should jump to the next test or if I should jump to is not a leap year.
回答1:
First, return values from functions go into eax
, so that's where the atoi
result will be. Then use div
like this:
xor edx,edx
div 4
Now the result of the division will be in eax
, and the remainder will be in edx
.
来源:https://stackoverflow.com/questions/36901647/isolating-remainder-in-nasm-program