【发布时间】:2021-08-29 21:45:48
【问题描述】:
我用 nasm x86 汇编代码编写 +-_ ,其中空白是用户输入。现在我已经有了它,但我需要它来阅读下一行并在那里做。我是汇编新手,我不想使用循环或指针或任何东西。也许是时代的一种方式? 这是我的代码:
segment .data
newline db 0xA, 0xD
newlinelen equ $-newline
segment .bss
num1 resb 2
num2 resb 2
num3 resb 2
;num4 resb 2
;num5 resb 2
;num6 resb 2
res resb 1
;res2 resb 1
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
;reading num 1
mov eax, 3
mov ebx, 0
mov ecx, num1
mov edx, 2
int 0x80
;reading num 2
mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 2
int 0x80
;reading num 3
mov eax, 3
mov ebx, 0
mov ecx, num3
mov edx, 2
int 0x80
;new line for next equation
mov edx, newline
mov ecx, newlinelen
mov ebx, 1
mov eax, 4
int 0x80
; moving the first number to eax register and second number to ebx
; and subtracting ascii '0' to convert it into a decimal number
mov eax, [num1]
sub eax, '0'
mov ebx, [num2]
sub ebx, '0'
mov ecx, [num3]
sub ecx, '0'
; add ebx to eax
add eax, ebx
; add '0' to to convert the sum from decimal to ASCII
add eax, '0'
; storing the sum in memory location res
mov [res], eax
;subtract ecx from eax
sub eax, ecx
mov [res], eax
; print the sum
mov eax, 4
mov ebx, 1
mov ecx, res
mov edx, 1
int 0x80
exit:
int 0x80
我的输入是 1+2-1,输出是 2,这是正确的。我不需要在 4 以上的任何数字或负数上使用它。 我试过用数字 4、5、6 重复整个过程,但结果只是给了我一个空白的 ascii 字符,这是我得到的最接近结果的字符。
;;new equation?
;reading num 4
mov eax, 3
mov ebx, 0
mov ecx, num4
mov edx, 2
int 0x80
;reading num 5
mov eax, 3
mov ebx, 0
mov ecx, num5
mov edx, 2
int 0x80
;reading num 6
mov eax, 3
mov ebx, 0
mov ecx, num6
mov edx, 2
int 0x80
; moving the first number to eax register and second number to ebx
; and subtracting ascii '0' to convert it into a decimal number
mov eax, [num4]
sub eax, '0'
mov ebx, [num5]
sub ebx, '0'
mov ecx, [num6]
sub ecx, '0'
; add ebx to eax
add eax, ebx
; add '0' to to convert the sum from decimal to ASCII
add eax, '0'
; storing the sum in memory location res
mov [res2], eax
;subtract ecx from eax
sub eax, ecx
mov [res2], eax
; print the sum
mov eax, 4
mov ebx, 1
mov ecx, res2
mov edx, 1
int 0x80
我也试过用 AL 代替 eax,用 AH 代替另一个方程,但它不像我也不明白的那样工作。然后我还尝试制作每个字节并放入段符号 resb 1,用于 + 符号,这不起作用。我只是希望它在之后再读一行。
【问题讨论】: