我用 nasm Assembly 编写了以下程序:
section .text
global _start:
_start:
; Input variables
mov edx, inLen
mov ecx, inMsg
mov ebx, 1
mov eax, 4
int 0x80
mov edx, 2
mov ecx, num1
mov ebx, 0
mov eax, 3
int 0x80
mov edx, inLen
mov ecx, inMsg
mov ebx, 1
mov eax, 4
int 0x80
mov edx, 2
mov ecx, num2
mov ebx, 0
mov eax, 3
int 0x80
; Put input values in correct registers
mov eax, [num1]
sub eax, '0' ; convert char to num
mov ebx, [num2]
sub ebx, '0' ; convert char to num
; Perform addition
add eax, ebx
add eax, '0' ; convert num to char
; Set sum in res
mov [res], eax
; Output result
mov edx, resLen
mov ecx, resMsg
mov ebx, 1
mov eax, 4
int 0x80
mov edx, 1
mov ecx, res
mov ebx, 1
mov eax, 4
int 0x80
; Exit program
mov eax, 1
int 0x80
section .bss
num1 resb 2
num2 resb 2
res resb 2
section .data
inMsg db "Input number: ", 0xA, 0xD
inLen equ $-inMsg
resMsg db "Result: ", 0xA, 0xD
resLen equ $-resMsg
当我运行它时,控制台如下所示:
tyler@ubuntu:~/ASM/Addition$ ./Add
Input number:
3
Input number:
2
Result:
5tyler@ubuntu:~/ASM/Addition$
我怎样才能得到它,以便 5 将打印在自己的行上,而不是在它后面直接打印 cmd? IE。它应该看起来像这样:
tyler@ubuntu:~/ASM/Addition$ ./Add
Input number:
3
Input number:
2
Result:
5
tyler@ubuntu:~/ASM/Addition$