Download presentation
Presentation is loading. Please wait.
1
A Hexadecimal Calculator
CALCUL.EXE A Hexadecimal Calculator
2
mov ah,0eh ;3- mov al,byte ptr hex_num+1 ;3- int 10h ;3- To display the MSB of v1
3
db = dw = db 12h db 34h dw h dw 9ah dw bch
4
ASCII Code Table
5
Signed & unsigned numbers
binary Decimal value 0111 0110 0101 0100 0011 0010 0001 0000 1111 1110 1101 1100 1011 1010 1001 1000 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 (-2^3, 2^2, 2^1, 2^0)
9
MOV AH,D ; D3H = Y MOV CH,F ; F1H = Y ADD AH,CH MOV BH,6D ; 6DH = Y SUB BH,CH AH = ? Y BH = ? Y
10
ASCII Code Table
11
; ###################
; # Procedure name: hex_dig ; # Function: To detect if al contains the ASCII code of a legal hexadecimal digit ; # Exit: CF = 0 if al is an ASCII code between '0' and '9', 'A' and 'F', or 'a' and 'f'. ; # CF = 1 otherwise ; # Register Changed: Flags (changed by CMP) hex_dig proc cmp al,'0' jb not_hex_dig cmp al,'9' jbe is_hex_dig not_0_to_9: cmp al,'A' cmp al,'F' not_A_to_F: cmp al,'a' cmp al,'f' not_hex_dig: stc ret is_hex_dig: clc hex_dig endp
12
ascii_to_hex proc cmp al,'0' jb not_hex_dig cmp al,'9' jbe is_0_to_9
; ########################################### ; # Procedure name: ascii_to_hex ; # Function: Convert an ASCII code (one byte) to hexadecimal number (one nibble plus a leading zero nibble) ; # Entry: AL = the ASCII-coded hexadecimal digit ; # Exit: AL = the hexadecimal nibble with leading zero nibble ; # CF = 0 if al is an ASCII code between '0' and '9', 'A' and 'F', or 'a' and 'f'. ; # CF = 1 otherwise ; # Register Changed: AL, Flags (changed by CMP) ; # Example: Entry:: AL = '8' = 38H Exit:: AL = 08H ascii_to_hex proc cmp al,'0' jb not_hex_dig cmp al,'9' jbe is_0_to_9 not_0_to_9: cmp al,'A' cmp al,'F' jbe is_A_to_F_big not_A_to_F_big: cmp al,'a' cmp al,'f' jbe is_a_to_f not_hex_dig: stc ret is_0_to_9: sub al,'0' jmp is_hex_dig is_A_to_F_big: sub al,'A'-10 is_a_to_f: sub al,'a'-10 is_hex_dig: clc ascii_to_hex endp
13
; ###########################################
; # Procedure name: ascii_2digit_to_hex_1byte ; # Function: Convert a 2-digit ASCII code (two bytes) to a 1-byte hexadecimal number ; # Entry: AH = the higher digit of two ASCII-coded hexadecimal digits ; # AL = the lower digit of two ASCII-coded hexadecimal digits ; # Exit: AL = the two digits of the hexadecimal number ; # Register Changed: AH, AL, Flags ; # Example: Entry AX = '6a'; Exit AL = 6ah. ascii_2digit_to_hex_1byte proc call ascii_to_hex ;4- Convert the lower digit of the ASCII-coded hexadecimal number to a hexadecimal nibble rol ax,8 ;5- To exchange AH and AL. call ascii_to_hex ;6- Convert the higher digit of the ASCII-coded hexadecimal number to a hexadecimal nibble rol al,4 ;7- Move the left nibble to the right or al,ah ;8- Combine the two nibble ret ascii_2digit_to_hex_1byte endp
14
; ###########################################
; # Procedure name: hex_to_ascii ; # Function: Convert a hexadecimal number (one nibble plus a leading zero nibble) to an ASCII code (one byte) ; # Entry: AL = the hexadecimal nibble with leading zero nibble ; # Exit: AL = the ASCII-coded hexadecimal digit ; # Register Changed: AL, Flags (changed by CMP) ; # Example: Entry:: AL = 08H Exit: AL = '8' = 38H ; # Register changed: AL, Flags (changed by CMP or ADD) hex_to_ascii proc add al,30h cmp al,3ah jb below_a add al,61h - 3ah below_a: ret hex_to_ascii endp
15
; ###########################################
; # Procedure name: hex_1byte_to_ascii_2digit ; # Function: Convert a 1-byte hexadecimal number to a 2-digit ASCII code (two bytes) ; # Entry: AL = the two digits of the hexadecimal number ; # Exit: AH = the higher digit of two ASCII-coded hexadecimal digits ; # AL = the lower digit of two ASCII-coded hexadecimal digits ; # Register Changed: AH, AL, Flags ; # Example: Entry AL = 6ah; Exit AX = '6a' hex_1byte_to_ascii_2digit proc mov ah,al and ah,0fh shr al,4 call hex_to_ascii ;4- Convert the higher hexadecimal nibble to an ASCII-coded hexadecimal byte rol ax,8 ;5- To exchange AH and AL. call hex_to_ascii ;6- Convert the lower hexadecimal nibble to an ASCII-coded hexadecimal byte ret hex_1byte_to_ascii_2digit endp
16
mov hex_num,'00' ; To initialize hex_num get_1dig: mov ah,0eh ;3-
; ########################################### ; # Procedure name: get_hex_num_2dig ; # Function: Receive a 2-digit hexdecimal number from the keyboard ; # Exit: AX = the hexadecimal number ; # Note: '0' will lead user input get_hex_num_2dig proc pusha mov hex_num,'00' ; To initialize hex_num get_1dig: mov ah,0eh ;3- mov al,byte ptr hex_num+1 ;3- int 10h ;3- To display the MSB of v1 mov al,byte ptr hex_num ;3- int 10h ;3- To display the LSB of v1 mov ah,0 ;2- int 16h ;2- To recieve a key from the keyboard call hex_dig ;4- To distinguish hex and non-hex key jc hex_num_in ;4- Any non-hex-dig key will be treated as an "Enter" key mov ah,byte ptr hex_num ;7- New hex_num = LSB of old hex_num * 10H + entered digit mov hex_num,ax ;7- To save the new hex_num mov ah,0eh ;6- mov al,bs ;6- int 10h ;6- To go back one space jmp get_1dig hex_num dw '00' hex_num_in: popa mov ax,hex_num ret get_hex_num_2dig endp
17
mov ah,0eh ;6- mov al,bs ;6- int 10h ;6- To go back one space jmp get_1dig hex_num dw '00' hex_num_in: popa mov ax,hex_num ret get_hex_num_2dig endp
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.