linux - 如何创建一个基本的 NASM Assembly (Linux) 计算器来添加两个整数?

标签 linux assembly x86 nasm

我尝试了一下,并尝试了:

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 结合使用。)

最佳答案

这不是关于“更正”您的代码,而是关于编写其中缺失的部分。

您必须解析整数,忽略分隔空格。添加它们,然后将结果转换为字符串。然后输出这个字符串。您可能无法接受调用 scanfstrtolprintf,因为它使用的是标准 C 库。

关于linux - 如何创建一个基本的 NASM Assembly (Linux) 计算器来添加两个整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14443566/

相关文章:

c++ - 将目录链接到标准输入

linux - Docker Redis 使用 -v 以持久存储启动时给出错误 (chown : changing ownership of '.' : Permission denied)

assembly - 一个操作码字节如何根据 "register/opcode"字段解码为不同的指令?那是什么?

c - 使用局部变量查找函数的返回地址

go - 易于阅读的 Golang 汇编输出?

linux - 调用 shell 脚本时,bash 中的 exec 命令不起作用

linux - 为什么我收到 "Unable to find the checksum in the image"错误?

c - TI-84 : call a function from the catalog with z88dk

c - for 循环中的实际指令

assembly - 如何阅读英特尔操作码符号