assembly 将输入的两个数字相加

zdwk9cvp  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(169)

我试着写一个小程序,它接受两个整数,把它们相加,然后显示给用户。我设法写了整个程序,并保存了变量(number1number2),现在我必须把number1 + number2存储在result中。
下面是代码。add函数的位置是什么?

segment .data 

    msg db "Please enter a digit ", 0xA,0xD 
    len equ $ - msg 

segment .bss

    number1 resb 10 
    number2 resb 10 
    result resb 10    

segment .text 

    msg2 db "Please enter a second digit", 0xA,0xD 
    len2 equ $ - msg2 

    msg3 db "The sum is : ", 0xA,0xD 
    len3 equ $ - msg3

global _start 

 _start: 

    mov eax, 4         
    mov ebx, 1         
    mov ecx, msg         
    mov edx, len 
    int 0x80                

    mov eax, 3 
    mov ebx, 0  
    mov ecx, number1 
    mov edx, 10 
    int 0x80            

    mov eax, 4        
    mov ebx, 1         
    mov ecx, msg2          
    mov edx, len2         
    int 0x80

    mov eax, 3  
    mov ebx, 0  
    mov ecx, number2 
    mov edx, 10 
    int 0x80        

    mov eax, 4         
    mov ebx, 1         
    mov ecx, msg3          
    mov edx, len3         
    int 0x80

    ;add result, number1
    ;add result, number2 

    mov eax, 4        
    mov ebx, 1 

    mov ecx, $result         
    mov edx, len3         
    int 0x80

exit:    
    mov eax, 1   
    xor ebx, ebx 
    int 0x80
von4xj4u

von4xj4u1#

首先,从输入中得到的数字是ASCII格式的,因此需要减去'0',得到实际的十进制值。在本例中,我假设每个数字都是1位数,我将缓冲区的大小设置为2字节(以读取数字+ '\n')。
一旦数字(如上所述的一位数)相加完毕,将'0'加到结果中,将十进制数转换成ASCII码。因为这听起来像是家庭作业,所以我不打算添加打印多位数的代码。
无论如何,我已经添加了几行代码来告诉你如何开始,并整理了一些混乱的东西。

SYS_EXIT  equ 1
    SYS_READ  equ 3
    SYS_WRITE equ 4
    STDIN     equ 0
    STDOUT    equ 1

segment .data 

    msg db "Please enter a digit ", 0xA,0xD 
    len equ $- msg 

segment .bss

    number1 resb 2 
    number2 resb 2 
    result resb 1    

segment .text 

    msg2 db "Please enter a second digit", 0xA,0xD 
    len2 equ $- msg2 

    msg3 db "The sum is: "
    len3 equ $- msg3

global _start 

 _start: 

    mov eax, SYS_WRITE         
    mov ebx, STDOUT         
    mov ecx, msg         
    mov edx, len 
    int 0x80                

    mov eax, SYS_READ 
    mov ebx, STDIN  
    mov ecx, number1 
    mov edx, 2
    int 0x80            

    mov eax, SYS_WRITE        
    mov ebx, STDOUT         
    mov ecx, msg2          
    mov edx, len2         
    int 0x80

    mov eax, SYS_READ  
    mov ebx, STDIN  
    mov ecx, number2 
    mov edx, 2
    int 0x80        

    mov eax, SYS_WRITE         
    mov ebx, STDOUT         
    mov ecx, msg3          
    mov edx, len3         
    int 0x80

    ; load number1 into eax and subtract '0' to convert from ASCII to decimal
    mov eax, [number1]
    sub eax, '0'
    ; do the same for number2
    mov ebx, [number2]
    sub ebx, '0'

    ; add eax and ebx, storing the result in eax
    add eax, ebx
    ; add '0' to eax to convert the digit from decimal to ASCII
    add eax, '0'

    ; store the result in result
    mov [result], eax

    ; print the result digit
    mov eax, SYS_WRITE        
    mov ebx, STDOUT
    mov ecx, result         
    mov edx, 1        
    int 0x80

exit:    
    mov eax, SYS_EXIT   
    xor ebx, ebx 
    int 0x80

相关问题