Convert decimal to binary using a loop and display it - EMU 8086 Assembly Language
In the code below, I am trying to convert the value to count to binary and then display it. The code doesn't seem to work and only displays 1 when it should display, for example 1000 for 8, but instead it displays one for each value. Any tips on what should I change or do to improve my code?
I also tried to display the counter, but gave up after an unsuccessful error and use N1
, N2
etc. to display numbers instead of using a loop, which will make my code a kilometer shorter and improve it. Please let me know :)
Suppose I want to start a counter from 0 or 1, the value I want to display, should it be in ascii code or hex decimal value? For example, I want to display the number 5 and then 6. (an example will be shown in the second code segment). Will Code 2 work? what needs to be changed to make it work? Should the ascii value be put into CX
? Should I use a memory variable instead?
CODE 2 NOTE: 2 dots represent code missing, please ask if you do not understand the code properly. Please donβt mind these questions, I am still a coding hobbyist and find it difficult to do the simplest things. Thanks for reading :) Here is the code for the whole program for people who need it: Full code here
CODE 1:
OUT1 MACRO COUNT ;new macro added, edit date 7th july
MOV CL,COUNT
MOV AL,CL
MOV DX,0378H
OUT DX,AL
ENDM
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
.
.
N0 DB '0','$'
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('$') ; ADDED NEW
COL1 DB 36 ; ADDED NEW
.
.
.code
.
MOV COUNT,5
.
.
BINARY: LEA SI,STR1
PUSH CX
MOV CL,4
BINL1: MOV AL,COUNT
CBW
DIV TWO
MOV BYTE PTR[SI],AH
INC SI
DEC CL
JNZ BINL1
POP CX
JMP PRINTBINARY
PRINTBINARY: MOV AH, BYTE PTR[SI]
PUSH CX
PUSH AX
MOV CL,4
PBIN1: CURSOR COL1,22 ; 36,22 then 35,22 to 33,22
CMP AH,0
JE ZERO
JNE ONE
ZERO: DISP N0
JMP X
ONE: DISP N1
x: DEC SI
DEC CL
JNZ PBIN1
POP AX
POP CX
JMP L0
CODE 2:
mov cx,5
disp cx
inc cx
disp cx
source to share
I have checked the linked code. The problem is that the conversion algorithm is from number ("count") to binary ("str1"). I made three changes: corruptdna should replace the following codes in the code and run it:
1. Added one more byte to the "str1" variable to keep the "$" at the end and display it with int 21h, ah = 9:
STR1 DB 5 DUP('$')
2. Changed the conversion algorithm from number ("count") to binary ("str1"). The division is not correct, I have shifted the right bits, converting each extracted bit to "0" or "1" and storing those characters in "str1":
BINARY: MOV SI,OFFSET STR1+3 ;POSITION OF THE LEAST SIGNIFICANT BIT (EXTREME RIGHT).
PUSH CX
MOV CL,4
MOV AL,COUNT
BINL1:
SHR AL, 1 ;PUSH BITS TO THE RIGHT. BIT 0 IN CARRY FLAG.
JC BIT_1 ;IF ( CARRY FLAG == 1 ) JUMP TO BIT_1.
MOV [ BYTE PTR SI ], '0' ;IF NO JUMP, BIT IS ZERO.
JMP NEXT_BIT ;SKIP "BIT_1".
BIT_1: MOV [ BYTE PTR SI ], '1'
NEXT_BIT:
DEC SI ;NEXT BIT POSITION IS TO THE LEFT.
DEC CL
JNZ BINL1
POP CX
JMP PRINTBINARY
3. Finally, display "str1" (end with '$'):
PRINTBINARY: CURSOR 36,22 ; 36,22 then 35,22 to 33,22
MOV AH, 9 ;SERVICE TO DISPLAY STRING '$' ENDED.
MOV DX, OFFSET STR1 ;STRING TO DISPLAY ('$' ENDED).
INT 21H
JMP L0
source to share