从输入中添加两个数字

时间:2011-10-26 00:06:36

标签: assembly x86

我正在尝试编写这个小程序,它接受2个整数并添加它们并将它们显示给用户。我设法编写整个程序并保存变量(number1number2),现在我必须将number1 + number2存储在result中。

这是代码。添加功能在哪里?

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  

1 个答案:

答案 0 :(得分:0)

首先,从输入获得的数字将是ASCII,因此您需要从它们中减去'0'以获得实际的十进制值。对于这种情况,我假设每个数字都是1位数,我已经将缓冲区设置为2个字节(读取数字+ '\n')。

添加数字(如所述的单个数字)后,将'0'添加到结果中,以便从十进制转换为ASCII。这显然只适用于0-9之间的结果;因为这听起来像是家庭作业我不会添加打印多位数的代码。

无论如何,我已经添加了几行来向您展示如何开始并整理您的一些混乱。你应该真的使用equates来处理系统调用数字。

    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