【发布时间】:2021-11-01 23:44:08
【问题描述】:
我正在尝试实现一个简单的加法计算器,但我无法将输入存储在我的数组中。我正在尝试逐个字符地读取字符,因为我想稍后使用它来为我的 B 编译器实现一个后端(它具有从标准输入逐个字符读取字符的 getchar 函数)。我的代码是闲置的:
segment .data
numb db 0, 0, 0, 0
indx db 0
char db '0'
newl db 0ah
msg1 db 'enter a number: '
len1 equ $ - msg1
segment .text
global _start ; defines the entry point
print: ; push msg; push len
pop eax ; removes caller address from stack
pop edx ; gets length
pop ecx ; gets msg
push eax ; pushes CA to stack again
mov ebx , 01h ; tells that it's an output call
mov eax , 04h ; system call (write)
int 80h ; calls it
ret
getc: ; push add; push len
pop eax ; removes caller address from stack
pop ecx ; gets ouput addrress
push eax ; pushes CA to stack again
mov edx , 01h
mov ebx , 00h ; tells that it's an input call
mov eax , 03h ; system call (read)
int 80h ; calls it
ret
exit:
mov ebx , 0 ; sets exit code
mov eax , 01h ; system call (exit)
int 80h ; calls it
_start:
push msg1
push len1
call print
read:
push char
call getc
mov eax , numb
add eax , indx
mov [eax], dword char
inc byte [indx]
mov eax , char
cmp eax , newl
jne read
jmp exit ; exits program
现在我只是尝试存储输入,因为我从完整代码中得到了段错误,所以我开始剥离代码,直到找到错误原因。
【问题讨论】:
-
add eax , indx- 您将两个地址相加,而不是对存储在那里的字节值进行零扩展。使用调试器单步执行,记住indx和[indx]之间的区别。您需要将movzx加载到另一个 reg 中,因为db比指针窄。 -
@PeterCordes 我想我明白了,但我无法检查,因为我收到了另一个问题(cmp/jne 没有按预期工作),我应该编辑帖子还是打开另一个问题?
标签: arrays assembly input x86 nasm