TITLE Convert Number to ASCII Format (convert.asm) .686 .MODEL flat, stdcall .STACK 4096 INCLUDE Irvine32.inc .data hexstring BYTE 9 DUP(?) binstring BYTE 33 DUP(?) .code main PROC mov eax, 123456789 mov edx, OFFSET hexstring call Convert2Hex call WriteString call Crlf ; Add instructions to test Convert2Bin, Convert2Dec, and Convert2Int exit main ENDP ; Convert number in EAX to ASCII hexadecimal format ; Store hexadecimal characters in the string passed by reference ; Receives: EAX = 32-bit number ; EDX = string address ; Returns: store converted hexadecimal characters Convert2Hex PROC push ebx ; save registers push ecx push edx mov ecx, 8 ; 8 iterations L1: rol eax, 4 ; rotate upper 4 bits of eax mov ebx, eax and ebx, 15 ; keep lower 4 bits in ebx mov bl, hexarray[ebx] ; convert 4 bits to Hex character mov [edx], bl ; store Hex char in string add edx, 1 ; point to next char in string loop L1 mov BYTE PTR [edx], 0 ; Terminate string with a NULL char pop edx ; restore register values pop ecx pop ebx ret ; return hexarray BYTE "0123456789ABCDEF" Convert2Hex ENDP ; Convert number in EAX to ASCII binary format ; Store '0' and '1' characters in the string passed by reference ; Receives: EAX = 32-bit number ; EDX = string address ; Returns: store converted binary characters Convert2Bin PROC ; to be completed Convert2Bin ENDP ; Convert unsigned number in EAX to ASCII decimal format ; Receives: EAX = 32-bit number ; EDX = string address ; Returns: Store characters in the string passed by reference Convert2Dec PROC pushad ; save all general-purpose registers mov esi, edx ; ESI = string address mov ecx, 0 ; counts decimal digits mov ebx, 10 ; divisor = 10 L1: mov edx, 0 ; dividend = EDX:EAX div ebx ; EDX = remainder digit = 0 to 9 (stored in DL) add dl, '0' ; convert DL to ASCII digit push dx ; save digit on the stack inc ecx ; count digit cmp eax, 0 jnz L1 ; loop back if EAX != 0 L2: pop dx ; last digit pushed is the most significant mov [esi], dl ; save ASCII digit in string inc esi loop L2 mov BYTE PTR [esi], 0 ; Terminate string with a NULL char popad ; restore all general-purpose registers ret ; return Convert2Dec ENDP ; Convert signed number in EAX to ASCII integer format prefixed with sign ; Receives: EAX = 32-bit number ; EDX = string address ; Returns: Store characters in the string passed by reference Convert2Int PROC ; to be completed Convert2Int ENDP END main