nibble_to_ASCII procedure

The last procedure that I have to implement is nibble_to_ASCII. For this one I came out with 2 possible implementations: the first one uses an "add-like" algorithm; the second one uses a "look-up table" algorithm. I implemented and tested both of them because I wanted to experiment and learn. In the end, I picked up the one that I found the better depending on certain criteria.

nibble_to_ASCII with "add-like" algorithm:
nibble_to_ASCII_add: ################################################################################ # # This procedure converts the value of a nibble (4 bits) in the corresponding # hexadecimal code. The function uses the low nibble of AL register. # # INPUT: AL = can be any value it uses only bit 3 to 0 # OUTPUT: AL = ASCII code # REGISTER USAGE: --- # THIS ONE CALLS: --- # ################################################################################ ### mask bit 7 to 4 before calculation for robustness and AL, 0x0f ### AL get increased by the ASCII value for '0' add AL, 48 # 48='0' ... 57='9' ### IF (AL > 57) cmp AL, 58 # flag = AL - 58 # 57='9' in ASCII, so if AL is now anywhere high # then 57 we are in hex range from 0xA to 0xF, # if so we have to add 7 to the calculation conversion. # it calculates AL-58 and sets sign flag, if negative # (SF = 1 -> set) we can skip the "then" instruction. jbe endif_001 # Jump if (CR or ZF) = 1. # JBE compares two unsigned integers. ### THEN add AL, 7 ### ENDIF endif_001: ##################### # end of procedure ##################### ret

nibble_to_ASCII with "look-up table" algorithm:
nibble_to_ASCII_xlat: ################################################################################ # # this function convert the value of a nibble (4 bits) in the corresponding # hexadecimal code. The function uses the low nibble of AL register. # # INPUT: AL = can be any value it uses only bit 3 to 0 # OUTPUT: AL = ASCII code # REGISTER USAGE: BX # THIS ONE CALLS: --- # ################################################################################ ### mask bit 7 to 4 before calculation for robustness and AX, 0x000f ### translate mov bx, translation_table: # set BX pointer cs: # use segment overwrite: # the default segment is DS, but the translation # table is within the same segment of the code # which is CS. xlat # translate: what it does is AL = CS:[BX + AL] ##################### # end of procedure ##################### ret ################################################################################ # The translation table: # # value [hex]: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f # ASCII [hex]: 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 # ################################################################################ translation_table: 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46

Comments