大会两位数数量减去

问题描述 投票:0回答:1

我对队打,我需要增加或减去两个2位数字(用户输入的数字)的项目。我得到了另外的工作。减法给出了十位数是正确的,但给出的个位数字的地方奇怪的字符。这里是我的意思(结果的图片):

Link To Flickr Screen Shot

我用:

  • TASM和TLINK
  • 86
  • Windows XP中(虚拟盒)

码:

.MODEL SMALL
.STACK 100h
.DATA 

    startMsg DB 13,10,'1.) Add ',10,'2.) Subtract',10,'3.) Exit',10,10,'Select a function: $'
    integer1Msg DB 13,10,10,'Enter the first integer: $'
    integer2Msg DB 13,10,'Enter the second integer: $'
    errorOccuredMsg DB 13,10,13,10,'An error occured, please try again! $'

    sumMsg DB 13,10,10,'The sum is: $'
    subMsg DB 13,10,10,'The differance is: $'

    gotNum  DB  0
    func    DB  0

.CODE
start:
    mov ax,@data
    mov ds,ax

    mov gotNum, 0   ; initialize var

    ;display & get the selection
    mov ah,09
    mov dx,OFFSET startMsg
    int 21h
    mov ah,01
    int 21h

    mov func,al

    ;check what the selection was
    cmp func,'1'
    je additionIntermediate
    cmp func,'2'
    je subtractionIntermediate
    cmp func,'3'
    je exit

exit:
    mov ah,4ch
    int 21h

getNum1:
    ;get the first integer (1st digit)
    mov ah,09
    mov dx,OFFSET integer1Msg
    int 21h
    mov ah,01
    int 21h

    ;check that input is a number
    cmp al, 30h  ;0 (ASCII 48)
    jl errorIntermediate  
    cmp al, 39h  ;9 (ASCII 57)
    jg errorIntermediate

    mov bh,al
    sub bh,30h

    ;get the first integer (2nd digit)
    mov ah,01
    int 21h

    ;check that input is a number
    cmp al, 30h  ;0 (ASCII 48)
    jl errorIntermediate
    cmp al, 39h  ;9 (ASCII 57)
    jg errorIntermediate

    mov bl,al
    sub bl,30h

    jmp getNum2

additionIntermediate:
    jmp addition

subtractionIntermediate:
    jmp subtraction

errorIntermediate:
    jmp errorOccured

getNum2:
    ;get the second integer
    mov ah,09
    mov dx,OFFSET integer2Msg
    int 21h
    mov ah,01
    int 21h

    ;check that input is a number
    cmp al, 30h  ;0 (ASCII 48)
    jl errorOccured  
    cmp al, 39h  ;9 (ASCII 57)
    jg errorOccured

    mov ch,al
    sub ch,30h

    ;get the second integer
    mov ah,01
    int 21h

    ;check that input is a number
    cmp al, 30h  ;0 (ASCII 48)
    jl errorOccured  
    cmp al, 39h  ;9 (ASCII 57)
    jg errorOccured

    mov cl,al
    sub cl,30h

    mov gotNum,1

    cmp func,'1'
    je addition
    cmp func,'2'
    je subtraction
    cmp func,'3'
    je errorOccured

getNumIntermediate:
    jmp getNum1

addition:
    cmp gotNum,0
    je getNumIntermediate

    ;add the two numbers and adjust for addition
    mov ah,0
    add bx,cx
    aaa
    or bx,3030h

    ;display result
    mov ah,09
    mov dx,OFFSET sumMsg
    int 21h
    mov dl,bh
    mov ah,02
    int 21h
    mov dl,bl
    mov ah,02
    int 21h

    ;return to beginning
    jmp start

errorOccured:
    lea dx, errorOccuredMsg
    mov ah,09
    int 21h
    jmp start

subtraction:
    cmp gotNum,0
    je getNumIntermediate

    ;determine which subtraction to use
    cmp bx,cx
    jg subtractionPos
    cmp bx,cx
    jl subtractionNeg

subtractionPos: ;integer1 is larger than integer2
    ;subtract
    sub bx,cx
    aas
    or bx,3030h

    ;display result
    mov ah,09
    mov dx,OFFSET subMsg
    int 21h
    mov dl,bh
    mov ah,02
    int 21h
    mov dl,bl
    mov ah,02
    int 21h

    ;return to beginning
    jmp start

subtractionNeg: ;integer2 is larger than integer1
    ;subtract
    sub cx,bx
    aas
    or cx,3030h

    ;display result
    mov ah,09
    mov dx, OFFSET subMsg
    int 21h
    mov ah,06
    mov dl,2dh ;displays the negative sign
    int 21h
    mov dl,ch
    mov ah,02
    int 21h
    mov dl,cl
    mov ah,02
    int 21h

    ;return to beginning
    jmp start

end start

我是很新的组件,因此任何意见将是巨大的。

编辑

;subtract
    sub bx,cx       
    mov ch,bh    ;store the value of bh
    xchg bx, ax
    mov bl,0Ah
    div bl
    xchg ah, al
    xchg ax, bx
    mov bh,ch    ;restore the value of bh
    or bx,3030h
assembly x86 addition subtraction tasm
1个回答
0
投票
;subtract
sub bx,cx
aas
or bx,3030h

AAS指令装置减法后调整的Al。它调整AL,不BX。要使用BX工作,你可以使用此代码:

sub bx, cx
xchg bx, ax
    mov bl, 0x0A
    div bl
    or ax, 0x3030
xchg ah, al
xchg ax, bx
© www.soinside.com 2019 - 2024. All rights reserved.