在隔离NASM剩余计划 [英] Isolating remainder in nasm program
问题描述
我目前正在写在x86 Linux的一个NASM计划,我想执行分割的第一个命令行参数(一年),由第一闰年检查的计算。我要检查,如果余数为0或没有,但我有如何做到这一点挣扎。我知道在div命令存储在某个寄存器中的答案,一个在另一个余但现在我只是使用测试。这里的code
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
我相信我需要测试RDI,fourTest改为采用div,但不知道如何隔离剩余,并决定,如果我要跳到下一个测试或者我是否应该跳到是不是闰年。
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.
推荐答案
首先,返回值从函数进入 EAX
,所以这其中与atoi
结果将是。然后使用 DIV
是这样的:
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
现在分工的结果将是 EAX
,其余的将在 EDX
。
Now the result of the division will be in eax
, and the remainder will be in edx
.
这篇关于在隔离NASM剩余计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!