0

I wrote a calculator for decimal and binary number systems. Added the ability to work with the hexadecimal number system. It turned out the following: when I perform some operation in the hexadecimal system (+ - / *), then if the result is hexadecimal, that is, letters from A to F, in this case, instead of letters, displays characters. For example: 7D00 * 2 = FA00, but the program displays ?:00, that is, instead of the letter F, displays the character ? , and instead of the letter A displays the symbol :

A - :
B - ;
C - <
D - =
E - >
F - ?

And how can I make the corresponding letter appear instead of a symbol?
This is the program code

LOCALS @@
.model tiny

.code
 .386
        org     100h
main    proc

        jmp     start

        OpMul           equ     '*'
        OpDiv           equ     '/'
        OpAdd           equ     '+'
        OpSub           equ     '-'

        CrLf            db      0Dh, 0Ah, '$'
        msgAbout        db      'Calc', 0Dh, 0Ah
                        db      'Used', 0Dh, 0Ah
                        db      'B - binary base', 0Dh, 0Ah
                        db      'D - decimal base', 0Dh, 0Ah
                        db      'H - hex base', 0Dh, 0Ah
                        db      '+, -, *, / - operations', 0Dh, 0Ah
                        db      '= or ENTER - show result and quit', 0Dh, 0Ah, '$'
        msgChangeBase2  db      08h, 08h, 08h, 08h, 08h
        msgBase2        db      ' (BIN)', '$'
        msgChangeBase10 db      08h, 08h, 08h, 08h, 08h
        msgBase10       db      ' (DEC)', '$'
        msgChangeBase16 db      08h, 08h, 08h, 08h, 08h
        msgBase16       db      ' (HEX)', '$'
        msgPromptExpr   db      0Dh, 0Ah, 'Expression: ', '$'
        msgResult       db      'Result: ', '$'
        msgPressAnyKey  db      0Dh, 0Ah, 'Press any key to exit...', '$'
        Base            dw      ?  
        EnBaseChange    dw      ? 
        Operand1        dw      ?  
        Operand2        dw      ? 
        Operation       db      ?
        OperationNext   db      ? 
start:
        mov     ah,     09h
        lea     dx,     [msgAbout]
        int     21h

        mov     ah,     09h
        lea     dx,     [msgPromptExpr]
        int     21h

        mov     [Operand1],     0
        mov     [Operand1],     0
        mov     [Operation],    OpAdd
        mov     [EnBaseChange], 1
        mov     [Base], 10
        mov     ah,     09h
        lea     dx,     [msgBase10]
        int     21h
        @@GetCmd:
                mov     ah,     00h
                int     16h
        @@IsDigit:
                cmp     al,     '0'
                jb      @@IsOperation

                ;;;;;;;;;;;;
                ;to enter letters from A to F for hexadecimal notation
                cmp     al,     '9'+1
                jb      @@1
                cmp     al,     'A'
                jb      @@IsOperation
                cmp     al,     'F'
                ja      @@IsOperation
                mov     dl,     al
                mov     ah,     0
                sub     al,     37h
                jmp @@2
                ;;;;;;;;;;;;

            @@1:mov     dl,     al
                mov     ah,     0
                sub     al,     '0'
            @@2:cmp     ax,     [Base]
                jae     @@GetCmd
                push    ax
                mov     ah,     02h
                int     21h
                pop     ax
                mov     [EnBaseChange], 0 
                mov     bx,     0
                mov     bl,     al
                mov     ax,     [Operand2]
                mul     [Base]
                add     ax,     bx
                mov     [Operand2],   ax
                jmp     @@GetCmd
        @@IsOperation:
                cmp     al,     'a'
                jb      @@case
                cmp     al,     'z'
                ja      @@case
                add     al,     'A'-'a'
        @@case:
                cmp     al,     0Dh
                jne     @@IsBase2
                mov     al,     '='

        @@IsBase2:
                cmp     al,     'B'
                jne     @@IsBase16
                cmp     [EnBaseChange], 1
                jne     @@GetCmd

                mov     [Base], 2
                mov     ah,     09h
                lea     dx,     [msgChangeBase2]
                int     21h
                jmp     @@GetCmd


        ;;;;;;;;;;;;;;;;;;;
        @@IsBase16:
                cmp     al,     'H'
                jne     @@IsBase10
                cmp     [EnBaseChange], 1
                jne     @@GetCmd

                mov     [Base], 16
                mov     ah,     09h
                lea     dx,     [msgChangeBase16]
                int     21h
                jmp     @@GetCmd
        ;;;;;;;;;;;;;;;;;;;
        @@IsBase10:
                cmp     al,     'D'
                jne     @@IsOp
                cmp     [EnBaseChange], 1
                jne     @@GetCmd

                mov     [Base], 10
                mov     ah,     09h
                lea     dx,     [msgChangeBase10]
                int     21h
                jmp     @@GetCmd
        @@IsOp:
                cmp     al,     OpAdd
                je      @@DoOperation
                cmp     al,     OpSub
                je      @@DoOperation
                cmp     al,     OpMul
                je      @@DoOperation
                cmp     al,     OpDiv
                je      @@DoOperation
                cmp     al,     '='
                je      @@DoOperation
                jmp     @@GetCmd
        @@DoOperation:
                mov     [OperationNext],        al
                mov     ax,     [Operand1]
                cwd
                mov     bx,     [Operand2]
                cmp     [Operation],    OpAdd
                jne     @@Sub
                add     ax,     bx
                jmp     @@Calc
        @@Sub:
                cmp     [Operation],    OpSub
                jne     @@Mul
                sub     ax,     bx
                jmp     @@Calc
        @@Mul:
                cmp     [Operation],    OpMul
                jne     @@Div
                imul    bx
        @@Div:
                cmp     [Operation],    OpDiv
                jne     @@Calc
                idiv    bx
        @@Calc:
                mov     [EnBaseChange], 1
                mov     [Operand1],     ax
                mov     [Operand2],     0
                mov     al,     [OperationNext]
                mov     [Operation],    al
                int     29h

                mov     ah,     09h
                lea     dx,     [msgBase2]
                cmp     [Base], 2

                mov     ah,     09h
                lea     dx,     [msgBase16]
                cmp     [Base], 16

                je      @@ShowBase
                lea     dx,     [msgBase10]
        @@ShowBase:
                int     21h
                mov     al,     [Operation]
                cmp     al,     '='
                je      @@Break
        jmp     @@GetCmd
@@Break:
        mov     ax,     [Operand1]
        mov     bx,     [Base]
        call    ShowUInt16
        mov     ah,     09h
        lea     dx,     [msgPressAnyKey]
        int     21h

        mov     ah,     00h
        int     16h

        int     20h

main    endp

ShowUInt16       proc
        push    ax
        push    bx
        push    cx
        push    dx
        ;mov     bx,     10    
        mov     cx,     0        
        @@div:
                xor     dx,     dx   
                div     bx
                add     dl,     '0' 
                push    dx    
                inc     cx          
                test    ax,     ax    
        jnz     @@div          
        @@show:
                mov     ah,     02h   
                pop     dx       
                int     21h       
        loop    @@show                 
        pop     dx
        pop     cx
        pop     bx
        pop     ax
        ret
ShowUInt16       endp

end     main
ProcgerXacker
  • 105
  • 1
  • 9
  • 1
    http://www.asciitable.com/ As you can see in that table, if the value is >= 0xA you need to add 55 (`'7'`) rather than 48 (`'0'`) to get the corresponding character. Or you could just have a lookup table, like `db "0123456789ABCDEF"`. – Michael Jun 11 '20 at 08:36
  • possible duplicate of [How to convert a binary integer number to a hex string?](https://stackoverflow.com/q/53823756) – Peter Cordes Jun 11 '20 at 10:14
  • I added this code: cmp dl, 10 jge a1 a1: add dl, '7' But now the opposite is happening. Hexademical counts normally, but the others don't. That is, now: 7D00+7D00=FA00, but the programm outputs FA77, that is, now it outputs numbers incorrectly, and in this case it outputs 7 instead of 0, and so on with the rest of the numbers – ProcgerXacker Jun 11 '20 at 16:52

0 Answers0