This code for binary output does not work properly - Assembly language

248 views Asked by At

in the code below, i am trying to push a value to jump binary which will then be displayed as binary code. For some reason the code seems to only display 1111 instead of dividing properly and showing the respective binary code for each decimal number in count. Any advice guys?

CURSOR  MACRO Col, Row
    MOV AH,02
    MOV BH,00
    MOV DL,Col
    MOV DH,Row
    INT 10H
  ENDM

DISP   MACRO MES
    MOV AH,09
    MOV DX,OFFSET MES
    INT 21H

.DATA
.
.
.

N1              DB      '1','$'  
N2              DB      '2','$'
N3              DB      '3','$'
N4              DB      '4','$'
N5              DB      '5','$'
N6              DB      '6','$'
N7              DB      '7','$'
N8              DB      '8','$'
N9              DB      '9','$'
COUNT           DB      0


.
.
.code
. 
.
.
.
MOV COUNT,5
.
.
BINARY: MOV AL,COUNT; 22,39 38 37 36
        CBW
        DIV TWO

        CURSOR 36,22
        CMP AH,0
        JE ZERO
        JNE ONE
ZERO:   DISP N0
        jmp x
ONE:    DISP N1
x:      
        CBW         
        DIV TWO

        CURSOR 35,22
        CMP AH,0
        JE ZERO1
        JNE ONE1
ZERO1:   DISP N0
        jmp x1
ONE1:    DISP N1
x1:      
        CBW         
        DIV TWO

        CURSOR 34,22
        CMP AH,0
        JE ZERO2
        JNE ONE2
ZERO2:   DISP N0
        jmp x2
ONE2:    DISP N1
x2:      
        CBW         
        DIV TWO

        CURSOR 33,22
        CMP AH,0
        JE ZERO3
        JNE ONE3
ZERO3:   DISP N0
        jmp x3
ONE3:    DISP N1
x3:      
        JMP L0
2

There are 2 answers

5
Fifoernik On BEST ANSWER

Here are some pointers to get you on your way.

  1. The MOV COUNT,5 instruction is placed between the data and so probably a reason to malfunction.

  2. Each time you use DIV N2 you divide by 50 because N2 was defined as a character. Best also define a number 2 with TWO db 2

  3. When you want to compare the remainder of the division you already have destroyed the AH register through the macro call CURSOR 39,22

  4. The second time you use DIV N2 the AX register no longer contain a usable value because the macro calls DISP N0 and DISP N1 have put the number 09h in the AH register.

  5. To succesfully do a cascade of 4 divisions you must preserve the quotient of each previous division and then use that as the dividend for the following division. See example below.

  6. When you use DISP N0 to display "0" I think that you need to jump over the instruction DISP N1 because now you are getting both "01".

An easy solution for problems 3 and 4 would be to save at least the AX register in both macros.

CURSOR  MACRO Col, Row
push ax
MOV AH,02
MOV BH,00
MOV DL,Col
MOV DH,Row
INT 10H
pop ax
ENDM

DISP   MACRO MES
push ax
MOV AH,09
MOV DX,OFFSET MES
INT 21H
pop ax
ENDM

Solution to the cascade (to be repeated for each division):

BINARY: MOV AL,COUNT; 22,39 38 37 36
        CBW
        DIV TWO
        push ax     ;Save the quotient in AL
        CURSOR 39,22
        CMP AH,0
        JE ZERO
        JNE ONE
ZERO:   DISP N0
        jmp x
ONE:    DISP N1
x:      pop ax      ;Restore the quotient in AL
        cbw         ;Extent before byte division
        DIV TWO
0
corruptdna On
    CURSOR  MACRO Col, Row
push ax
MOV AH,02
MOV BH,00
MOV DL,Col
MOV DH,Row
INT 10H
pop ax
ENDM

DISP   MACRO MES
push ax
MOV AH,09
MOV DX,OFFSET MES
INT 21H
pop ax
ENDM


.DATA
.
.
.

N1              DB      '1','$'  
N2              DB      '2','$'
N3              DB      '3','$'
N4              DB      '4','$'
N5              DB      '5','$'
N6              DB      '6','$'
N7              DB      '7','$'
N8              DB      '8','$'
N9              DB      '9','$'
COUNT           DB      0
TWO             DB      2
STR1            DB      4 DUP('$')

.
.
.code
. 
.
.
.
MOV COUNT,5
.
.
BINARY: MOV AL,COUNT; 22,39 38 37 36
        CBW
        DIV TWO
        push ax     ;Save the quotient in AL
        CURSOR 36,22
        CMP AH,0
        JE ZERO
        JNE ONE
ZERO:   DISP N0
        jmp x
ONE:    DISP N1
x:      pop ax      ;Restore the quotient in AL
        CBW         ;Extent before byte division
        DIV TWO
        push ax     ;Save the quotient in AL
        CURSOR 35,22
        CMP AH,0
        JE ZERO1
        JNE ONE1
ZERO1:   DISP N0
        jmp x1
ONE1:    DISP N1
x1:      pop ax      ;Restore the quotient in AL
        CBW         ;Extent before byte division
        DIV TWO
        push ax     ;Save the quotient in AL
        CURSOR 34,22
        CMP AH,0
        JE ZERO2
        JNE ONE2
ZERO2:   DISP N0
        jmp x2
ONE2:    DISP N1
x2:      pop ax      ;Restore the quotient in AL
        CBW         ;Extent before byte division
        DIV TWO
        push ax     ;Save the quotient in AL
        CURSOR 33,22
        CMP AH,0
        JE ZERO3
        JNE ONE3
ZERO3:   DISP N0
        jmp x3
ONE3:    DISP N1
x3:      pop ax      ;Restore the quotient in AL
        JMP L0