首页 > 解决方案 > NASM 汇编 - 将用户输入的两个数字相乘

问题描述

我对组装非常陌生,我已经花了几个小时试图弄清楚这一点。我正在制作的程序应该从 0 到 9 取两个数字,然后将它们相乘并显示正确答案(例如:9 乘以 3 是 27)。我需要用“空间号空间号”的符号从用户输入两个数字(例如“5 3”)。我尝试在没有输入的情况下编写代码(对数字进行硬编码)并且效果很好,但是当我尝试使数字来自输入时,结果是错误的。该程序应该从 0 到 9 取两个数字,然后将它们相乘。我真的不知道出了什么问题......任何帮助都将非常感激。

当我输入:“ 5 3”时,它输出“?4”(应该是15)

当我输入:“ 9 3”时,它仍然输出“?4”(应该是27)

当我输入:“5 3”时,它输出“?6”

当我输入:“9 3”时,它输出“??4”

当我检查 var1 和 var2 (通过在读取它们后打印它们)时,数字是正确的。如,如果输入“5 3”,则 var1 会打印 5,而 var2 会打印 3。即使更改了空格,var1 和 var2 也会打印正确的数字(因此,如果我输入“5 3”,则 var1 仍然是 5,而 var2仍然是 3)。

我的教授刚刚解释了如何获得输入,但我不能 100% 确定它是如何工作的。我觉得奇怪的是输出根据空格的位置而有所不同,因为 var1 和 var2 都应该接受 2 个字节。

我的代码不正确:

;this program multiplies two numbers input by the user, being able to solve with two digits
;the notation for the input should be space number space number (ex:" 9 3")


section .data

section .bss

    var1 resb 2
    var2 resb 2
    result resb 2
    tensdigit resb 2
    onesdigit resb 2
    
section .text
    global _start

_start:
    ;reads & saves user input for var1
    mov eax, 3
    mov ebx, 0
    mov ecx, var1
    mov edx, 2  ;2 bytes of info 
    int 80h
    


    ;reads & saves user input for var2
    mov eax, 3
    mov ebx, 0
    mov ecx, var2
    mov edx, 2  ;2 bytes of info 
    int 80h


    mov al, [var1]
   ; sub al, '0' ;not sure if this is needed or not

    mov bl, [var2]
   ; sub bl, '0'

   ;multiply bl by al
    mul bl
   ; sub ax, '0'
    mov [result], ax
   

                         
    ;divide by ten
    mov ax, 0 ;set the register to 0! Very important to avoid errors
    mov ax, [result]
    mov bx, 10
    div bx ;divide by ten
    
    mov [tensdigit], al ;this should have 1 if using 12 (2 if 24, etc)
    
    mov ax, [result]
    sub ax, 10
    mov [result], ax ;move new number into result
    
    mov al, [result]
    mov bl, 10
    div bl
    ;find the remainder
    mov [onesdigit], ah
    
    add [tensdigit], word '0' ;add 0 to ensure it's considered a number
    add [result], word '0'
    add [onesdigit], word '0'

    ;print tensdigit
    mov eax, 4
    mov ebx, 1
    mov ecx, tensdigit
    mov edx, 2
    int 80h 
    
    ;print onesdigit
    mov eax, 4
    mov ebx, 1
    mov ecx, onesdigit
    mov edx, 2
    int 80h 


    ;system exit
    mov eax,1           
    mov ebx,0   
    int 80h;

当数字被硬编码时,这是我的代码(这个正确地乘以数字):

;this program adds two numbers, being able to solve with two digits

section .data

section .bss
    var1 resb 2
    var2 resb 2
    result resb 2
    tensdigit resb 2
    onesdigit resb 2
    
section .text
    global _start

_start:


    ;setup the variables
    mov [var1], word 9
    mov [var2], word 3
    mov al, [var1]
    mov bl, [var2]
    mul bl
    mov [result], ax

    
                         
    ;divide by ten
    mov ax, 0 ;set the register to 0! Very important to avoid errors
    mov ax, [result]
    mov bx, 10
    div bx ;divide by ten
    
    mov [tensdigit], al ;this should have 1 if using 12 (2 if 24, etc)
    
    mov ax, [result]
    sub ax, 10
    mov [result], ax ;move new number into result
    
    mov al, [result]
    mov bl, 10
    div bl
    ;find the remainder
    mov [onesdigit], ah
    
    add [tensdigit], word '0' ;add 0 to ensure it's considered a number
    add [result], word '0'
    add [onesdigit], word '0'

    ;print tensdigit
    mov eax, 4
    mov ebx, 1
    mov ecx, tensdigit
    mov edx, 2
    int 80h 
    
    ;print onesdigit
    mov eax, 4
    mov ebx, 1
    mov ecx, onesdigit
    mov edx, 2
    int 80h 


    mov eax,1           
    mov ebx,0   
    int 80h;

标签: linuxassemblyx86nasm

解决方案


推荐阅读