计算器汇编语言 - Linux x86的&放大器; NASM - 科 [英] Calculator in Assembly Language - Linux x86 & NASM - Division

查看:353
本文介绍了计算器汇编语言 - Linux x86的&放大器; NASM - 科的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用汇编语言计算器在x86处理器上执行。

基本上,我的计算器会询问用户输入两个数字,然后指示哪些操作(加法,减法,乘法和除法)想与他们做。

我的计算器的加,减和乘正确,但无法划分。在制作师,我总是得到1的结果。

然后我离开我的应用程序code完整的:

 部分。数据    ;消息    MSG1 DB 10' - 计算器 - ',10,0
    lmsg1 EQU $ - MSG1    MSG2 10分贝,1号:,0
    lmsg2 EQU $ - MSG2    消息3分贝'数2:0
    lmsg3 EQU $ - 消息3    MSG4分贝10,'1。添加,10,0
    lmsg4 EQU $ - MSG4    MSG5分贝'2。减去,10,0
    lmsg5 EQU $ - MSG5    MSG6 DB3。乘,10,0
    lmsg6 EQU $ - MSG6    MSG7 DB'4。鸿沟,10,0
    lmsg7 EQU $ - MSG7    msg8 DB'操作:',0
    lmsg8 EQU $ - msg8    msg9 DB 10,结果:,0
    lmsg9 EQU $ - msg9    MSG10 10分贝,无效选项,10,0
    lmsg10 EQU $ - MSG10    nlinea分贝10,10,0
    lnlinea EQU $ - nlinea.bss段    ;保留的空间,用于存储由用户提供的值。    OPC RESB 2
    NUM1 RESB 2
    NUM2 RESB 2
    结果RESB 2.text段    全球_start_开始:    ;打印屏幕上的消息1
    MOV EAX,4
    MOV EBX,1
    MOV ECX中,MSG
    MOV EDX,lmsg1
    INT 80H    ;打印屏幕上的消息2
    MOV EAX,4
    MOV EBX,1
    MOV ECX,MSG 2
    MOV EDX,lmsg2
    INT 80H    ;我们得到NUM1值。
    MOV EAX,3
    MOV EBX,0
    MOV ECX,NUM1
    MOV EDX,2
    INT 80H    ;打印屏幕上的消息3
    MOV EAX,4
    MOV EBX,1
    MOV ECX,消息3
    MOV EDX,lmsg3
    INT 80H    ;我们得到NUM2值。
    MOV EAX,3
    MOV EBX,0
    MOV ECX,NUM2
    MOV EDX,2
    INT 80H    ;打印屏幕上的信息4
    MOV EAX,4
    MOV EBX,1
    MOV ECX,MSG4
    MOV EDX,lmsg4
    INT 80H    ;打印屏幕上的信息5
    MOV EAX,4
    MOV EBX,1
    MOV ECX,MSG5
    MOV EDX,lmsg5
    INT 80H    ;打印屏幕上的消息6
    MOV EAX,4
    MOV EBX,1
    MOV ECX,MSG6
    MOV EDX,lmsg6
    INT 80H    ;打印屏幕上的信息7
    MOV EAX,4
    MOV EBX,1
    MOV ECX,MSG7
    MOV EDX,lmsg7
    INT 80H    ;打印屏幕上的消息8
    MOV EAX,4
    MOV EBX,1
    MOV ECX,msg8
    MOV EDX,lmsg8
    INT 80H    ;我们得到选择的选项。
    MOV EBX,0
    MOV ECX,OPC
    MOV EDX,2
    MOV EAX,3
    INT 80H    MOV啊,[OPC]移动选定的选项注册表啊
    子啊,'0';从ASCII转换为十进制    ;我们比较由用户输入的值,以知道执行什么操作。    CMP啊,1
    加济
    CMP啊,2
    JE减
    CMP啊,3
    JE乘法
    CMP啊,4
    JE鸿沟    ;如果由用户输入的值不符合上述任何
    ;条件那么我们显示错误信息,我们关闭程序。
    MOV EAX,4
    MOV EBX,1
    MOV ECX,MSG10
    MOV EDX,lmsg10
    INT 80H    JMP出口加:
    ;我们保持数在寄存器EAX和EBX
    MOV EAX,[NUM1]
    MOV EBX,[NUM2]    ;从ASCII转换为十进制
    子EAX,'0'
    子EBX,'0'    ;加
    添加EAX,EBX    ;从十进制转换为ASCII
    添加EAX,'0'    ;我们移动的结果
    MOV [结果],EAX    ;打印屏幕上的信息9
    MOV EAX,4
    MOV EBX,1
    MOV ECX,msg9
    MOV EDX,lmsg9
    INT 80H    ;打印屏幕上的结果
    MOV EAX,4
    MOV EBX,1
    MOV ECX,导致
    MOV EDX,1
    INT 80H    ;我们结束程序
    JMP出口减去:
    ;我们保持数在寄存器EAX和EBX
    MOV EAX,[NUM1]
    MOV EBX,[NUM2]    ;从ASCII转换为十进制
    子EAX,'0'
    子EBX,'0'    ;减去
    子EAX,EBX    ;从十进制转换为ASCII
    添加EAX,'0'    ;我们移动的结果
    MOV [结果],EAX    ;打印屏幕上的信息9
    MOV EAX,4
    MOV EBX,1
    MOV ECX,msg9
    MOV EDX,lmsg9
    INT 80H    ;打印屏幕上的结果
    MOV EAX,4
    MOV EBX,1
    MOV ECX,导致
    MOV EDX,1
    INT 80H    ;我们结束程序
    JMP出口乘:    ;我们店的数量在寄存器AX和BX
    MOV AX,[NUM1]
    MOV BX,[NUM2]    ;从ASCII转换为十进制
    子斧头,'0'
    子BX,'0'    ;乘。 AL = AX点¯xBX
    MUL BX    ;从十进制转换为ASCII
    加人,0    ;我们移动的结果
    MOV [结果],人    ;打印屏幕上的信息9
    MOV EAX,4
    MOV EBX,1
    MOV ECX,msg9
    MOV EDX,lmsg9
    INT 80H    ;打印屏幕上的结果
    MOV EAX,4
    MOV EBX,1
    MOV ECX,导致
    MOV EDX,1
    INT 80H    ;我们结束程序
    JMP出口划分:
    ;在该标签是误差!    ;我们店的数量在寄存器AX和BX
    MOV DX,0
    MOV AX,[NUM1]
    MOV BX,[NUM2]    ;从ASCII转换到decimall
    子斧头,'0'
    子BX,'0'
    ;师。 AX = DX:AX / BX
    DIV BX    ;从十进制转换为ASCII
    添加斧头,'0'
    ;我们移动的结果
    MOV [结果],斧    ;打印屏幕上的信息9
    MOV EAX,4
    MOV EBX,1
    MOV ECX,msg9
    MOV EDX,lmsg9
    INT 80H    ;打印屏幕上的结果
    ;始终打印1
    MOV EAX,4
    MOV EBX,1
    MOV ECX,导致
    MOV EDX,1
    INT 80H    ;我们结束程序
    JMP出口出口:
    ;打印屏幕上的两条新线
    MOV EAX,4
    MOV EBX,1
    MOV ECX,nlinea
    MOV EDX,lnlinea
    INT 80H
    ;结束该程序
    MOV EAX,1
    MOV EBX,0
    INT 80H

错误必须在标签中找到鸿沟。

为什么我总是得到1作为一个部门的结果?

我希望有人有更多的经验可以帮助我。


谢谢大家了。我终于计算器工作。这是我最后的code:

 部分。数据    ;消息    MSG1 DB 10' - 计算器 - ',10,0
    lmsg1 EQU $ - MSG1    MSG2 10分贝,1号:,0
    lmsg2 EQU $ - MSG2    消息3分贝'数2:0
    lmsg3 EQU $ - 消息3    MSG4分贝10,'1。添加,10,0
    lmsg4 EQU $ - MSG4    MSG5分贝'2。减去,10,0
    lmsg5 EQU $ - MSG5    MSG6 DB3。乘,10,0
    lmsg6 EQU $ - MSG6    MSG7 DB'4。鸿沟,10,0
    lmsg7 EQU $ - MSG7    msg8 DB'操作:',0
    lmsg8 EQU $ - msg8    msg9 DB 10,结果:,0
    lmsg9 EQU $ - msg9    MSG10 10分贝,无效选项,10,0
    lmsg10 EQU $ - MSG10    nlinea分贝10,10,0
    lnlinea EQU $ - nlinea.bss段    ;保留的空间,用于存储由用户提供的值。    OPC:2 RESB
    NUM1:2 RESB
    NUM2:2 RESB
    结果:2 RESB.text段    全球_start_开始:    ;打印屏幕上的消息1
    MOV EAX,4
    MOV EBX,1
    MOV ECX中,MSG
    MOV EDX,lmsg1
    INT 80H    ;打印屏幕上的消息2
    MOV EAX,4
    MOV EBX,1
    MOV ECX,MSG 2
    MOV EDX,lmsg2
    INT 80H    ;我们得到NUM1值。
    MOV EAX,3
    MOV EBX,0
    MOV ECX,NUM1
    MOV EDX,2
    INT 80H    ;打印屏幕上的消息3
    MOV EAX,4
    MOV EBX,1
    MOV ECX,消息3
    MOV EDX,lmsg3
    INT 80H    ;我们得到NUM2值。
    MOV EAX,3
    MOV EBX,0
    MOV ECX,NUM2
    MOV EDX,2
    INT 80H    ;打印屏幕上的信息4
    MOV EAX,4
    MOV EBX,1
    MOV ECX,MSG4
    MOV EDX,lmsg4
    INT 80H    ;打印屏幕上的信息5
    MOV EAX,4
    MOV EBX,1
    MOV ECX,MSG5
    MOV EDX,lmsg5
    INT 80H    ;打印屏幕上的消息6
    MOV EAX,4
    MOV EBX,1
    MOV ECX,MSG6
    MOV EDX,lmsg6
    INT 80H    ;打印屏幕上的信息7
    MOV EAX,4
    MOV EBX,1
    MOV ECX,MSG7
    MOV EDX,lmsg7
    INT 80H    ;打印屏幕上的消息8
    MOV EAX,4
    MOV EBX,1
    MOV ECX,msg8
    MOV EDX,lmsg8
    INT 80H    ;我们得到选择的选项。
    MOV EBX,0
    MOV ECX,OPC
    MOV EDX,2
    MOV EAX,3
    INT 80H    MOV啊,[OPC]移动选定的选项注册表啊
    子啊,'0';从ASCII转换为十进制    ;我们比较由用户输入的值,以知道执行什么操作。    CMP啊,1
    加济
    CMP啊,2
    JE减
    CMP啊,3
    JE乘法
    CMP啊,4
    JE鸿沟    ;如果由用户输入的值不符合上述任何
    ;条件那么我们显示错误信息,我们关闭程序。
    MOV EAX,4
    MOV EBX,1
    MOV ECX,MSG10
    MOV EDX,lmsg10
    INT 80H    JMP出口加:
    ;我们保持数在寄存器A1和B1
    MOV人,[NUM1]
    MOV BL,[NUM2]    ;从ASCII转换为十进制
    子等,0
    子BL,'0'    ;加
    加人,BL    ;从十进制转换为ASCII
    加人,0    ;我们移动的结果
    MOV [结果],人    ;打印屏幕上的信息9
    MOV EAX,4
    MOV EBX,1
    MOV ECX,msg9
    MOV EDX,lmsg9
    INT 80H    ;打印屏幕上的结果
    MOV EAX,4
    MOV EBX,1
    MOV ECX,导致
    MOV EDX,2
    INT 80H    ;我们结束程序
    JMP出口减去:
    ;我们保持数在寄存器A1和B1
    MOV人,[NUM1]
    MOV BL,[NUM2]    ;从ASCII转换为十进制
    子等,0
    子BL,'0'    ;减去
    子AL,BL    ;从十进制转换为ASCII
    加人,0    ;我们移动的结果
    MOV [结果],人    ;打印屏幕上的信息9
    MOV EAX,4
    MOV EBX,1
    MOV ECX,msg9
    MOV EDX,lmsg9
    INT 80H    ;打印屏幕上的结果
    MOV EAX,4
    MOV EBX,1
    MOV ECX,导致
    MOV EDX,1
    INT 80H    ;我们结束程序
    JMP出口乘:    ;我们存储的数字在寄存器A1和B1
    MOV人,[NUM1]
    MOV BL,[NUM2]    ;从ASCII转换为十进制
    子等,0
    子BL,'0'    ;乘。 AX =的Al x BL
    MUL BL    ;从十进制转换为ASCII
    添加斧头,'0'    ;我们移动的结果
    MOV [结果],斧    ;打印屏幕上的信息9
    MOV EAX,4
    MOV EBX,1
    MOV ECX,msg9
    MOV EDX,lmsg9
    INT 80H    ;打印屏幕上的结果
    MOV EAX,4
    MOV EBX,1
    MOV ECX,导致
    MOV EDX,1
    INT 80H    ;我们结束程序
    JMP出口划分:    ;我们店的数量在寄存器AX和BX
    MOV人,[NUM1]
    MOV BL,[NUM2]    MOV DX,0
    MOV啊,0    ;从ASCII转换到decimall
    子等,0
    子BL,'0'    ;师。 AL = AX / BX
    DIV BL    ;从十进制转换为ASCII
    添加斧头,'0'
    ;我们移动的结果
    MOV [结果],斧    ;打印屏幕上的信息9
    MOV EAX,4
    MOV EBX,1
    MOV ECX,msg9
    MOV EDX,lmsg9
    INT 80H    ;打印屏幕上的结果
    MOV EAX,4
    MOV EBX,1
    MOV ECX,导致
    MOV EDX,1
    INT 80H    ;我们结束程序
    JMP出口出口:
    ;打印屏幕上的两条新线
    MOV EAX,4
    MOV EBX,1
    MOV ECX,nlinea
    MOV EDX,lnlinea
    INT 80H
    ;结束该程序
    MOV EAX,1
    MOV EBX,0
    INT 80H


解决方案

您正在阅读的两个字节(字符)到 NUM1 NUM2 为您的号码输入。这通常将是单个数字(0-9),你是在打字和一个换行符。当你去做手术,你看每2个字节写入斧和BX,所以如果 NUM1 是5和 NUM2 为1,斧头将0xa35和BX将0xa31。然后,减去每鸿沟为0x30,在所有情况下给予1,您然后将其转换成0x31 1 和打印。

现在在其他情况下(添加/分),你实际装载4个字节到EAX和EBX。所以,当您添加 5 1 ,你会获得EAX和0xa310a35在EBX 0X ???? 0a31 (该????来自任何碰巧在结果)。然而,减去每一个的0x30和添加后,EAX的最低字节将0×06,所以你' LL打印 6 为你忽略了什么是高位字节。

I am making a calculator in assembly language to be executed on an x86 processor.

Basically, my calculator asks the user to enter two numbers and then to indicate which operation (addition, subtraction, multiplication and division) want to do with them.

My calculator adds, subtracts and multiplies correctly but is unable to divide. In making a division, I always get 1 as the result.

Then I leave my application code complete:

section .data

    ; Messages

    msg1        db      10,'-Calculator-',10,0
    lmsg1       equ     $ - msg1

    msg2        db      10,'Number 1: ',0
    lmsg2       equ     $ - msg2

    msg3        db      'Number 2: ',0
    lmsg3       equ     $ - msg3

    msg4        db      10,'1. Add',10,0
    lmsg4       equ     $ - msg4

    msg5        db      '2. Subtract',10,0
    lmsg5       equ     $ - msg5

    msg6        db      '3. Multiply',10,0
    lmsg6       equ     $ - msg6

    msg7        db      '4. Divide',10,0
    lmsg7       equ     $ - msg7

    msg8        db      'Operation: ',0
    lmsg8       equ     $ - msg8

    msg9        db      10,'Result: ',0
    lmsg9       equ     $ - msg9

    msg10       db      10,'Invalid Option',10,0
    lmsg10      equ     $ - msg10

    nlinea      db      10,10,0
    lnlinea     equ     $ - nlinea

section .bss

    ; Spaces reserved for storing the values ​​provided by the user.

    opc         resb    2
    num1        resb    2
    num2        resb    2
    result      resb    2

section .text

    global _start

_start:

    ; Print on screen the message 1
    mov eax, 4
    mov ebx, 1
    mov ecx, msg1
    mov edx, lmsg1
    int 80h

    ; Print on screen the message 2
    mov eax, 4
    mov ebx, 1
    mov ecx, msg2
    mov edx, lmsg2
    int 80h

    ; We get num1 value.
    mov eax, 3
    mov ebx, 0
    mov ecx, num1
    mov edx, 2
    int 80h

    ; Print on screen the message 3
    mov eax, 4
    mov ebx, 1
    mov ecx, msg3
    mov edx, lmsg3
    int 80h

    ; We get num2 value.
    mov eax, 3
    mov ebx, 0
    mov ecx, num2
    mov edx, 2
    int 80h

    ; Print on screen the message 4
    mov eax, 4
    mov ebx, 1
    mov ecx, msg4
    mov edx, lmsg4
    int 80h

    ; Print on screen the message 5
    mov eax, 4
    mov ebx, 1
    mov ecx, msg5
    mov edx, lmsg5
    int 80h

    ; Print on screen the message 6
    mov eax, 4
    mov ebx, 1
    mov ecx, msg6
    mov edx, lmsg6
    int 80h

    ; Print on screen the message 7
    mov eax, 4
    mov ebx, 1
    mov ecx, msg7
    mov edx, lmsg7
    int 80h

    ; Print on screen the message 8
    mov eax, 4
    mov ebx, 1
    mov ecx, msg8
    mov edx, lmsg8
    int 80h

    ; We get the option selected.
    mov ebx,0
    mov ecx,opc
    mov edx,2
    mov eax,3
    int 80h

    mov ah, [opc]   ; Move the selected option to the registry ah
    sub ah, '0'     ; Convert from ascii to decimal

    ; We compare the value entered by the user to know what operation to perform.

    cmp ah, 1
    je add
    cmp ah, 2
    je subtract
    cmp ah, 3
    je multiply
    cmp ah, 4
    je divide

    ; If the value entered by the user does not meet any of the above 
    ; conditions then we show an error message and we close the program.
    mov eax, 4
    mov ebx, 1
    mov ecx, msg10
    mov edx, lmsg10
    int 80h

    jmp exit

add:
    ; We keep the numbers in the registers eax and ebx
    mov eax, [num1]
    mov ebx, [num2]

    ; Convert from ascii to decimal
    sub eax, '0'
    sub ebx, '0'

    ; Add
    add eax, ebx

    ; Conversion from decimal to ascii
    add eax, '0'

    ; We move the result
    mov [result], eax

    ; Print on screen the message 9
    mov eax, 4
    mov ebx, 1
    mov ecx, msg9
    mov edx, lmsg9
    int 80h

    ; Print on screen the result
    mov eax, 4
    mov ebx, 1
    mov ecx, result
    mov edx, 1
    int 80h

    ; We end the program
    jmp exit

subtract:
    ; We keep the numbers in the registers eax and ebx
    mov eax, [num1]
    mov ebx, [num2]

    ; Convert from ascii to decimal
    sub eax, '0'
    sub ebx, '0'

    ; Subtract
    sub eax, ebx

    ; Conversion from decimal to ascii
    add eax, '0'

    ; We move the result
    mov [result], eax

    ; Print on screen the message 9
    mov eax, 4
    mov ebx, 1
    mov ecx, msg9
    mov edx, lmsg9
    int 80h

    ; Print on screen the result
    mov eax, 4
    mov ebx, 1
    mov ecx, result
    mov edx, 1
    int 80h

    ; We end the program
    jmp exit

multiply:

    ; We store the numbers in registers ax and bx
    mov ax, [num1]
    mov bx, [num2]

    ; Convert from ascii to decimal
    sub ax, '0'
    sub bx, '0'

    ; Multiply. AL = AX x BX
    mul bx

    ; Conversion from decimal to ascii
    add al, '0'

    ; We move the result
    mov [result], al

    ; Print on screen the message 9
    mov eax, 4
    mov ebx, 1
    mov ecx, msg9
    mov edx, lmsg9
    int 80h

    ; Print on screen the result
    mov eax, 4
    mov ebx, 1
    mov ecx, result
    mov edx, 1
    int 80h

    ; We end the program
    jmp exit

divide:
    ; IN THIS LABEL IS THE ERROR!

    ; We store the numbers in registers ax and bx
    mov dx, 0
    mov ax, [num1]
    mov bx, [num2]

    ; Convert from ascii to decimall
    sub ax, '0'
    sub bx, '0'
    ; Division. AX = DX:AX / BX
    div bx

    ; Conversion from decimal to ascii
    add ax, '0'
    ; We move the result
    mov [result], ax

    ; Print on screen the message 9
    mov eax, 4
    mov ebx, 1
    mov ecx, msg9
    mov edx, lmsg9
    int 80h

    ; Print on screen the result
    ; ALWAYS PRINTS 1
    mov eax, 4
    mov ebx, 1
    mov ecx, result
    mov edx, 1
    int 80h

    ; We end the program
    jmp exit

exit:
    ; Print on screen two new lines
    mov eax, 4
    mov ebx, 1
    mov ecx, nlinea
    mov edx, lnlinea
    int 80h
    ; End the program
    mov eax, 1
    mov ebx, 0
    int 80h

The error must be found inside the tag "divide".

Why do I always get 1 as result of a division?

I hope that someone with more experience can help me with this.


Thank you all very much. My calculator finally works. Here is my final code:

    section .data

    ; Messages

    msg1        db      10,'-Calculator-',10,0
    lmsg1       equ     $ - msg1

    msg2        db      10,'Number 1: ',0
    lmsg2       equ     $ - msg2

    msg3        db      'Number 2: ',0
    lmsg3       equ     $ - msg3

    msg4        db      10,'1. Add',10,0
    lmsg4       equ     $ - msg4

    msg5        db      '2. Subtract',10,0
    lmsg5       equ     $ - msg5

    msg6        db      '3. Multiply',10,0
    lmsg6       equ     $ - msg6

    msg7        db      '4. Divide',10,0
    lmsg7       equ     $ - msg7

    msg8        db      'Operation: ',0
    lmsg8       equ     $ - msg8

    msg9        db      10,'Result: ',0
    lmsg9       equ     $ - msg9

    msg10       db      10,'Invalid Option',10,0
    lmsg10      equ     $ - msg10

    nlinea      db      10,10,0
    lnlinea     equ     $ - nlinea

section .bss

    ; Spaces reserved for storing the values ​​provided by the user.

    opc:        resb    2
    num1:       resb    2
    num2:       resb    2
    result:     resb    2

section .text

    global _start

_start:

    ; Print on screen the message 1
    mov eax, 4
    mov ebx, 1
    mov ecx, msg1
    mov edx, lmsg1
    int 80h

    ; Print on screen the message 2
    mov eax, 4
    mov ebx, 1
    mov ecx, msg2
    mov edx, lmsg2
    int 80h

    ; We get num1 value.
    mov eax, 3
    mov ebx, 0
    mov ecx, num1
    mov edx, 2
    int 80h

    ; Print on screen the message 3
    mov eax, 4
    mov ebx, 1
    mov ecx, msg3
    mov edx, lmsg3
    int 80h

    ; We get num2 value.
    mov eax, 3
    mov ebx, 0
    mov ecx, num2
    mov edx, 2
    int 80h

    ; Print on screen the message 4
    mov eax, 4
    mov ebx, 1
    mov ecx, msg4
    mov edx, lmsg4
    int 80h

    ; Print on screen the message 5
    mov eax, 4
    mov ebx, 1
    mov ecx, msg5
    mov edx, lmsg5
    int 80h

    ; Print on screen the message 6
    mov eax, 4
    mov ebx, 1
    mov ecx, msg6
    mov edx, lmsg6
    int 80h

    ; Print on screen the message 7
    mov eax, 4
    mov ebx, 1
    mov ecx, msg7
    mov edx, lmsg7
    int 80h

    ; Print on screen the message 8
    mov eax, 4
    mov ebx, 1
    mov ecx, msg8
    mov edx, lmsg8
    int 80h

    ; We get the option selected.
    mov ebx,0
    mov ecx,opc
    mov edx,2
    mov eax,3
    int 80h

    mov ah, [opc]       ; Move the selected option to the registry ah
    sub ah, '0'     ; Convert from ascii to decimal

    ; We compare the value entered by the user to know what operation to perform.

    cmp ah, 1
    je add
    cmp ah, 2
    je subtract
    cmp ah, 3
    je multiply
    cmp ah, 4
    je divide

    ; If the value entered by the user does not meet any of the above
    ; conditions then we show an error message and we close the program.
    mov eax, 4
    mov ebx, 1
    mov ecx, msg10
    mov edx, lmsg10
    int 80h

    jmp exit

add:
    ; We keep the numbers in the registers al and bl
    mov al, [num1]
    mov bl, [num2]

    ; Convert from ascii to decimal
    sub al, '0'
    sub bl, '0'

    ; Add
    add al, bl

    ; Conversion from decimal to ascii
    add al, '0'

    ; We move the result
    mov [result], al

    ; Print on screen the message 9
    mov eax, 4
    mov ebx, 1
    mov ecx, msg9
    mov edx, lmsg9
    int 80h

    ; Print on screen the result
    mov eax, 4
    mov ebx, 1
    mov ecx, result
    mov edx, 2
    int 80h

    ; We end the program
    jmp exit

subtract:
    ; We keep the numbers in the registers al and bl
    mov al, [num1]
    mov bl, [num2]

    ; Convert from ascii to decimal
    sub al, '0'
    sub bl, '0'

    ; Subtract
    sub al, bl

    ; Conversion from decimal to ascii
    add al, '0'

    ; We move the result
    mov [result], al

    ; Print on screen the message 9
    mov eax, 4
    mov ebx, 1
    mov ecx, msg9
    mov edx, lmsg9
    int 80h

    ; Print on screen the result
    mov eax, 4
    mov ebx, 1
    mov ecx, result
    mov edx, 1
    int 80h

    ; We end the program
    jmp exit

multiply:

    ; We store the numbers in registers al and bl
    mov al, [num1]
    mov bl, [num2]

    ; Convert from ascii to decimal
    sub al, '0'
    sub bl, '0'

    ; Multiply. AX = AL x BL
    mul bl

    ; Conversion from decimal to ascii
    add ax, '0'

    ; We move the result
    mov [result], ax

    ; Print on screen the message 9
    mov eax, 4
    mov ebx, 1
    mov ecx, msg9
    mov edx, lmsg9
    int 80h

    ; Print on screen the result
    mov eax, 4
    mov ebx, 1
    mov ecx, result
    mov edx, 1
    int 80h

    ; We end the program
    jmp exit

divide:

    ; We store the numbers in registers ax and bx
    mov al, [num1]
    mov bl, [num2]

    mov dx, 0
    mov ah, 0

    ; Convert from ascii to decimall
    sub al, '0'
    sub bl, '0'

    ; Division. AL = AX / BX
    div bl

    ; Conversion from decimal to ascii
    add ax, '0'
    ; We move the result
    mov [result], ax

    ; Print on screen the message 9
    mov eax, 4
    mov ebx, 1
    mov ecx, msg9
    mov edx, lmsg9
    int 80h

    ; Print on screen the result
    mov eax, 4
    mov ebx, 1
    mov ecx, result
    mov edx, 1
    int 80h

    ; We end the program
    jmp exit

exit:
    ; Print on screen two new lines
    mov eax, 4
    mov ebx, 1
    mov ecx, nlinea
    mov edx, lnlinea
    int 80h
    ; End the program
    mov eax, 1
    mov ebx, 0
    int 80h

解决方案

You're reading two bytes (characters) into num1 and num2 for your number input. This will generally be a single digit (0-9) that you are typing in and a newline character. When you go to do an operation, you read 2 bytes each into ax and bx, so if num1 was 5 and num2 was 1, ax will be 0xa35 and bx will be 0xa31. You then subtract 0x30 from each and divide, giving 1 in all cases, which you then convert to 0x31 '1' and print.

Now in other cases (add/sub), you're actually loading 4 bytes into eax and ebx. So when you add 5 and 1, you'll get 0xa310a35 in eax and 0x????0a31 in ebx (the ???? comes from whatever happened to be in result.) However, after subtracting 0x30 from each and adding, the lowest byte of eax will 0x06, so you'll print 6 as you ignore what is in the upper bytes.

这篇关于计算器汇编语言 - Linux x86的&放大器; NASM - 科的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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