从输入中添加两个数字 [英] Adding Two Numbers from Input

查看:92
本文介绍了从输入中添加两个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to write this little program which accepts 2 integers and adds them and displays them to the user. I manage to write the entire program and save the variables (number1, number2), and now I have to store number1 + number2 in 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  

推荐答案

首先,您从输入中获得的数字将为ASCII,因此您需要从中减去'0'以获得实际的十进制值.对于这种情况,我假设每个数字的长度为1位,并且使缓冲区的大小为2个字节(以读取数字+ '\n').

For a start, the digits you get from the input will be in ASCII, so you're going to need to subtract '0' from them to get the actual decimal value. For this case I'm assuming each number is 1 digit long and I've made the buffers 2 bytes in size (to read the digit + '\n').

一旦添加了数字(如所述的单个数字),则将'0'添加到结果以从十进制转换为ASCII.显然,这仅适用于0-9之间的结果;由于这听起来像是作业,所以我不会添加将打印具有多个数字的数字的代码.

Once the numbers (single digits as stated) have been added, add '0' to the result to convert from decimal to ASCII. This will obviously only work for a result between 0-9; since this sounds like it might be homework I'm not going to add code that'll print numbers with multiple digits.

无论如何,我已经添加了几行内容来向您展示如何入门并消除一些混乱.您应该真正将equals用作系统调用号之类的东西.

Anyway I've added a few lines to show you how to get started and neatened up some of your mess. You should really use equates for things like syscall numbers.

    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

这篇关于从输入中添加两个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆