HEXADECIMAL TO BINARY
.model small
.stack 100h
.data
.code
main proc
mov bl, 0abh
mov cx, 08
again:
;Rotate bits of BL one position to the left
rol bl, 1
;Extract and print least significant bit
mov dl, bl
and dl, 01
add dl, 48
mov ah, 02h
int 21h
;Repeat loop 8 times
loop again
; Exit program
mov ah, 4ch
int 21h
main endp
end main
BINARY TO HEXADECIMAL (16 BITS)
.Model small
.stack 100h
.data
.code
Main proc
; initialize binary number and loop counter
mov bx, 0001011111111010b
mov cx, 04
Again:
; rotate bits of bx four positions to the left
rol bx, 04
; extract least significant nibble into dl
mov dl, bl
and dl, 0fh
; comparing with 9
cmp dl, 09
; if it exceed jmp to conversion
jg conversion
; convert binary value to ascii equivalent(1 - 9)
add dl, 48
jmp print
Conversion:
; convert output value to ascii equivalent(a - f)
add dl, 55
jmp print
Print:
; print hexadecimal digit
mov ah, 02h
int 21h
; repeat loop four times
loop again
; exit program
mov ah, 4ch
int 21h
Main endp
End main
BINARY TO HEXADECIMAL (8 BITS)
.Model small
.stack 100h
.data
.code
Main proc
; initialize binary number and loop counter
mov bL, 00010111b
mov cx, 02
Again:
; rotate bits of bx four positions to the left
rol bL, 04
; extract least significant nibble into dl
mov dl, bl
and dl, 0fh
; comparing with 9
cmp dl, 09
; if it exceed jmp to conversion
jg conversion
; convert binary value to ascii equivalent(1 - 9)
add dl, 48
jmp print
Conversion:
; convert output value to ascii equivalent(a - f)
add dl, 55
jmp print
Print:
; print hexadecimal digit
mov ah, 02h
int 21h
; repeat loop four times
loop again
; exit program
mov ah, 4ch
int 21h
Main endp
End main
No 3:
.model small
.stack 100h
.data
msg1 db 'Enter the first number (0-9): $'
msg2 db 'Enter the second number (0-9): $'
msg3 db 'Enter 1 for subtraction and 2 for addition: $'
msg4 db 'result: $'
num1 db ?
num2 db ?
sum db ?
.code
Main proc
mov ax, @data
mov ds, ax
; Read first number
mov dx, offset msg1
mov ah, 09h
int 21h
mov ah, 01h
int 21h
sub al, 30h
mov num1, al
; Read second number
mov dx, offset msg2
mov ah, 09h
int 21h
mov ah, 01h
int 21h
sub al, 30h
mov num2, al
; Read user's choice
mov dx, offset msg3
mov ah, 09h
int 21h
mov ah, 01h
int 21h
; Perform addition or subtraction
cmp al, '1'
je subtraction
cmp al, '2'
je addition
addition:
mov al, num1
add al, num2
add al, 30h
mov sum, al
jmp print_result
subtraction:
mov al, num1
sub al, num2
add al, 30h
mov sum, al
jmp print_result
print_result:
; Read first number
mov dx, offset msg4
mov ah, 09h
int 21h
; Print result
mov dl, sum
mov ah, 02h
int 21h
; Exit program
mov ah, 4ch
int 21h
Main endp
end Main