gpt4 book ai didi

assembly - 汇编中的程序求解表达式

转载 作者:行者123 更新时间:2023-12-04 20:38:39 24 4
gpt4 key购买 nike

我在汇编中的简单程序有问题。我正在使用 DOSBox 和 TASM。问题是第 76、78 和 80 行中的操作数类型不匹配。这是在乘法之后。我尝试通过使用不同的可变大小来进行一些更改。

; --------------------------------------------
; Equation=(a+c*b)/d-2*c,
; --------------------------------------------
.model small
.stack 100h
.data
a db 0
b db 0
c db 0
d db 0
result1 db ?
result2 db ?



message1 db "Equation: (a+c*b)/d-2*c a=$"
message2 db "b=$"
message3 db "c=$"
message4 db "d=$"
message5 db "Result=$"
.code

start: mov ax,@data
mov ds,ax

mov ax, seg message1 ;get a and save to a variable
mov ds,ax
mov dx,offset message1
mov ah, 9h
int 21h
mov ah, 1h
int 21h
sub al,30h ;converting to real number
mov a,al

mov ax, seg message2 ;get b and save to a variable
mov ds,ax
mov dx,offset message2
mov ah, 9h
int 21h
mov ah, 1h
int 21h
sub al,30h ;converting to real number
mov b,al


mov ax, seg message3 ;get c and save to a variable
mov ds,ax
mov dx,offset message3
mov ah, 9h
int 21h
mov ah, 1h
int 21h
sub al,30h ;converting to real number
mov c,al


mov ax, seg message4 ;get d and save to a variable
mov ds,ax
mov dx,offset message4
mov ah, 9h
int 21h
mov ah, 1h
int 21h
sub al,30h ;converting to real number
mov d,al


mov al,b ; (a+c*b) ------------------------error
mul c
add ax,a ; ------------------------error

push ax ;save current ax

mov ax,c ;d-2*c------------------------error
shl ax,2
sub d,ax


pop bx ;get previous ax to bx

div bx ; div ax:bx

mov result1,al
mov result2,ah

add result1,30h ;converting to string
add result2,30h ;converting to string

mov al,result1
mov bl,result2

mov ax, seg message5
mov ds,ax
mov dx,offset message5
mov ah, 9h
int 21h
mov al,result1
mov bl,result2
mov dl, al
mov ah , 2h
int 21h
mov dl, bl
mov ah , 2h
int 21h

mov ax,4C00h
int 21h

end start

最佳答案

你的程序几乎很好,你只有操作数大小的一些问题,这是正常的。所以我拿了你的代码并做了一些小改动,这些改动用箭头 (<========) 注释和指出,它们是:

  • 修复了操作数大小问题。我仍然使用 DB,因为我注意到您将数字捕获为单个字符。
  • (d-2*c) 的结果存储在 BX 中。这是因为我们需要除以 (a+c*b)/(d-2*c),并且您在 BX 中弹出 (a+c*b),因此,当您执行 div bx 时你在做 (d-2*c)/(a+c*b) 。
  • 将商和余数分开显示。
  • 已添加 13,10消息的换行符。
  • 已修复 shl ax,2来自 shl ax,1 .一 shl是 x2,两个 shl是 x2x2。
  • 余数从 dl 获得因为当 div用一个词做除数,余数留在dx .

  • 这是您的代码有一些小的变化(在 EMU8086 上测试):
    ; --------------------------------------------
    ; Equation=(a+c*b)/d-2*c,
    ; --------------------------------------------.model small
    .stack 100h
    .data
    a db 0
    b db 0
    c db 0
    d db 0
    result1 db ?
    result2 db ?



    message1 db 13,10,"Equation: (a+c*b)/d-2*c",13,10,"a=$"
    message2 db 13,10,"b=$" ;<================= 13,10 IS
    message3 db 13,10,"c=$" ;<================= LINEBREAK.
    message4 db 13,10,"d=$" ;<=================
    message5 db 13,10,"Quotient=$" ;<=================
    message6 db 13,10,"Remainder=$" ;<=================
    .code

    start: mov ax,@data
    mov ds,ax



    mov ax, seg message1 ;get a and save to a variable
    mov ds,ax
    mov dx,offset message1
    mov ah, 9h
    int 21h
    mov ah, 1h
    int 21h
    sub al,30h ;converting to real number
    mov a,al

    mov ax, seg message2 ;get b and save to a variable
    mov ds,ax
    mov dx,offset message2
    mov ah, 9h
    int 21h
    mov ah, 1h
    int 21h
    sub al,30h ;converting to real number
    mov b,al


    mov ax, seg message3 ;get c and save to a variable
    mov ds,ax
    mov dx,offset message3
    mov ah, 9h
    int 21h
    mov ah, 1h
    int 21h
    sub al,30h ;converting to real number
    mov c,al


    mov ax, seg message4 ;get d and save to a variable
    mov ds,ax
    mov dx,offset message4
    mov ah, 9h
    int 21h
    mov ah, 1h
    int 21h
    sub al,30h ;converting to real number
    mov d,al


    mov al,b ; (a+c*b)
    mul c
    mov cl,A ;<======== MOV A TO CX TO
    mov ch,0 ;<======== ADD IT TO AX.
    add ax,CX ;<======== C*B + A.

    ;push ax ;<======== NO LONGER NECESSARY BECAUSE
    ;<======== IN NEXT BLOCK WE USE BX.

    mov bl,C ;<======== MOV C TO BL AND CLEAR
    mov bh,0 ;<======== BH. NOW C IS IN BX.
    shl bx,1 ;<======== 2*c. ONE SHIFT IS x2, TWO SHIFTS ARE x2x2.
    sub d,bl ;d - 2c
    mov bl,d ;<======== MOV D TO BL AND CLEAR BH. NOW
    mov bh,0 ;<======== D IS IN BX. BX = (D-2C).

    ;pop ax ;<======== NO LONGER NECESSARY. AX CONTAINS (A+C*B).

    mov dx,0 ;<======== CLEAR DX, BECAUSE DIVISOR IS A WORD.
    ;<======== WHEN DIVISOR IS A WORD, DIV USES DX:AX.
    div bx ;<======== dx:ax / bx == DX:(A+C*B) / (D-2C). THIS
    ;<======== DIVISION IS UNSIGNED, FOR SIGNED USE IDIV.

    mov result1,al ;<===== QUOTIENT.
    mov result2,dl ;<===== REMAINDER, BECAUSE DIVISOR IS WORD.

    add result1,30h ;converting to string
    add result2,30h ;converting to string

    mov al,result1
    mov bl,result2

    ;DISPLAY QUOTIENT <=============
    mov ax, seg message5
    mov ds,ax
    mov dx,offset message5
    mov ah, 9h
    int 21h
    mov al,result1
    mov dl, al
    mov ah , 2h
    int 21h
    ;DISPLAY REMAINDER <=============
    mov ax, seg message6
    mov ds,ax
    mov dx,offset message6
    mov ah, 9h
    int 21h
    mov dl, bl
    mov ah , 2h
    int 21h

    mov ax,4C00h
    int 21h

    end start

    接下来是您的“待办事项” list :
  • 将操作数的大小从 DB 更改为 DW,以允许您的程序处理更大的数字。
  • 将 DIV 更改为 IDIV,因为 DIV 未签名,而 IDIV 已签名。 IDIV 会让您处理负面结果。
  • 使用 int=21h ah=0Ah 捕获数字作为字符串(而不是单个字符)。稍后,您将字符串转换为数字。接下来的两个链接将带您到将字符串转换为数字的过程:

  • Assembly x86 Date to Number - Breaking a string into smaller sections

    32 bit Calculator in 8086 Assembly

    最后,测试数据:
    (a+c*b) / (d-2*c)

    a=1
    b=2
    c=3
    d=8

    a+c*b = 1+3*2 = 7
    d-2*c = 8-2*3 = 2

    7 / 2 = quotient 3, remainder 1

    关于assembly - 汇编中的程序求解表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30645621/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com