I am trying to build a program in emu8086 which would be given as input 1 8 bit binary number then display in the output the hexadecimal form of it.
My code is this:
data segment
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
mov ax,data
mov ds,ax
mov cx,0
mov bl,8
input:
mov ah,07h
int 21h
cmp al,46
sub al,30h
cmp al,0
je valid
cmp al,1
je valid
jmp input
valid:
cmp bl,0
je exit
sub bl,1
shl cl,1
add cl,al
jmp input
exit:
mov bl,15
and bl,cl
cmp bl,0
je printzero
cmp bl,1
je printone
cmp bl,2
je printtwo
cmp bl,3
je printthree
cmp bl,4
je printfour
cmp bl,5
je printfive
cmp bl,6
je printsix
cmp bl,7
je printseven
cmp bl,8
je printeight
cmp bl,9
je printnine
cmp bl,10
je printa
cmp bl,11
je printb
cmp bl,12
je printc
cmp bl,13
je printd
cmp bl,14
je printe
cmp bl,15
je printf
printzero:
mov dl,'0'
mov ah,02h
int 21h
jmp exit2
printone:
mov dl,'1'
mov ah,02h
int 21h
jmp exit2
printtwo:
mov dl,'2'
mov ah,02h
int 21h
jmp exit2
printthree:
mov dl,'3'
mov ah,02h
int 21h
jmp exit2
printfour:
mov dl,'4'
mov ah,02h
int 21h
jmp exit2
printfive:
mov dl,'5'
mov ah,02h
int 21h
jmp exit2
printsix:
mov dl,'6'
mov ah,02h
int 21h
jmp exit2
printseven:
mov dl,'7'
mov ah,02h
int 21h
jmp exit2
printeight:
mov dl,'8'
mov ah,02h
int 21h
jmp exit2
printnine:
mov dl,'9'
mov ah,02h
int 21h
jmp exit2
printa:
mov dl,'A'
mov ah,02h
int 21h
jmp exit2
printb:
mov dl,'B'
mov ah,02h
int 21h
jmp exit2
printc:
mov dl,'C'
mov ah,02h
int 21h
jmp exit2
printd:
mov dl,'D'
mov ah,02h
int 21h
jmp exit2
printe:
mov dl,'E'
mov ah,02h
int 21h
jmp exit2
printf:
mov dl,'F'
mov ah,02h
int 21h
jmp exit2
exit2:
mov bl,240
and bl,cl
cmp bl,0
je printzero2
cmp bl,1
je printone2
cmp bl,2
je printtwo2
cmp bl,3
je printthree2
cmp bl,4
je printfour2
cmp bl,5
je printfive2
cmp bl,6
je printsix2
cmp bl,7
je printseven2
cmp bl,8
je printeight2
cmp bl,9
je printnine2
cmp bl,10
je printa2
cmp bl,11
je printb2
cmp bl,12
je printc2
cmp bl,13
je printd2
cmp bl,14
je printe2
cmp bl,15
je printf2
printzero2:
mov dl,'0'
mov ah,02h
int 21h
jmp exit3
printone2:
mov dl,'1'
mov ah,02h
int 21h
jmp exit3
printtwo2:
mov dl,'2'
mov ah,02h
int 21h
jmp exit3
printthree2:
mov dl,'3'
mov ah,02h
int 21h
jmp exit3
printfour2:
mov dl,'4'
mov ah,02h
int 21h
jmp exit3
printfive2:
mov dl,'5'
mov ah,02h
int 21h
jmp exit3
printsix2:
mov dl,'6'
mov ah,02h
int 21h
jmp exit3
printseven2:
mov dl,'7'
mov ah,02h
int 21h
jmp exit3
printeight2:
mov dl,'8'
mov ah,02h
int 21h
jmp exit3
printnine2:
mov dl,'9'
mov ah,02h
int 21h
jmp exit3
printa2:
mov dl,'A'
mov ah,02h
int 21h
jmp exit3
printb2:
mov dl,'B'
mov ah,02h
int 21h
jmp exit3
printc2:
mov dl,'C'
mov ah,02h
int 21h
jmp exit3
printd2:
mov dl,'D'
mov ah,02h
int 21h
jmp exit3
printe2:
mov dl,'E'
mov ah,02h
int 21h
jmp exit3
printf2:
mov dl,'F'
mov ah,02h
int 21h
jmp exit3
exit3:
mov ax, 4c00h
int 21h
ends
end start
I quickly realised that you dont need to store every input in a array , you can shift left by 1 the value of the register which would store the number then add the input (1 or 0).Well I expected my code to work but I came into a issue:
The register which stores the value of my number is CL.And it does correctly if I type 11110000 in this order the value of CL becomes F0 which is what I want.I put it through a AND mask of Fh to extract the 4 LSB and then through a AND mask of F0h to extract the 4 MSB however in the output I get 00.I honestly dont know where I am wrong.The value of BL register becomes the same with the value of the CL register but it still doesnt work.
Necessary corrections
cmp al,46instruction serves no purpose. It is a left-over from some previous edit: just remove it.You can simplify the checking, as well as the looping:
andwith 240 does is remove the Low Nibble from the BL register, but the High Nibble still remains where it was. This means that the value in BL is in {0,16,32,48,64,80,96,112,128,144,160,176,192,208,224,240}.The quick fix then is to replace the series of
cmp bl,?instructions:First steps in improvement
Currently your program contains a lot of repeated instructions. Better solutions exist and I'm sure that the links provided by @PeterCordes will reveal these.
Nonetheless, short of doing the whole task myself, I suggest you study the below code and that you find out for yourself that this already simplifies the program a lot, and that it demonstrates how to write readable code. Mind you, I don't present this as the optimal solution:
Final steps in improvement
Because your program encompasses eight counts of character input from the keyboard as well as two counts of character output to the screen, an optimized version would focus on code-size rather than execution time. The time spent in those ten DOS functions represents almost 100% of the execution time!
Due to all the repeated instructions, your program currently contains 471 bytes. My 'first steps' solution already lowered this number to 255 bytes, but surely we can do (much) better?
Let's look at what the program does. You input eight binary digits, combine these into a single byte-sized value, and then immediately after, you decompose the byte into its two nibbles.
Very often a (considerable) speed gain or size reduction comes from radically changing the methodology we use. What I propose is to not have to decompose a byte into its two nibbles. The below snippets repeat twice, the input of only four binary digits that are converted into one hex character.
These optimized snippets were assembled using FASM.