我尝试了一下,并尝试了:
section .data
promptmsg: db 'Enter integer: '
msgsize: equ $-promptmsg
section .bss ;creating variables to store input
firstnum: resb 6
secondnum: resb 6
section .text
global _start
_start:
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
mov eax, 4 ;system call to write
mov ebx, 1
mov ecx, promptmsg
mov edx, msgsize
int 80h
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
mov eax, 3 ;system call to read
mov ebx, 0
mov ecx, firstnum
mov edx, 6
int 80h
push firstnum
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
mov eax, 4 ;system call to write
mov ebx, 1
mov ecx, promptmsg
mov edx, msgsize
int 80h
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
mov eax, 3 ;system call to read
mov ebx, 0
mov ecx, secondnum
mov edx, 6
int 80h
push secondnum
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
pop eax
pop ebx
add eax, ebx ;attempt to add firstnum and secondnum and store in EAX
push eax
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
mov eax, 4 ;once again a system call to write
mov ebx, 1
pop ecx
mov edx, 7
int 80h
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
mov eax, 1 ;exit safely and return 0
mov ebx, 0
int 80h
对于所有 XOR 指令,我很抱歉,我只是想确保寄存器在我将它们用于系统调用之前被清除,我仍在学习汇编并且不确定哪些指令会使寄存器为空。
当我编译、链接和运行它时,只有包含 ENTER 字符(即换行符)时,我才能输入两个整数。无法将整数添加到其中,所以这已经导致了问题。所以当我输入它们时,程序的屏幕上没有进一步的输出,它就结束了。
我该如何纠正它?
(在这种情况下,不能将 C 或 C++ 与 ASM 结合使用。)
最佳答案
这不是关于“更正”您的代码,而是关于编写其中缺失的部分。
您必须解析整数,忽略分隔空格。添加它们,然后将结果转换为字符串。然后输出这个字符串。您可能无法接受调用 scanf
或 strtol
或 printf
,因为它使用的是标准 C 库。
关于linux - 如何创建一个基本的 NASM Assembly (Linux) 计算器来添加两个整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14443566/