I have been trying to convert an input integer into a binary ASCII CODE, but i have no idea how to do that. I make a len function to know the size of the input to know when to stop. The main idea of the code is to convert a number like 123 to a ASCII CODE like this 00110001 00110010 00110011. I want to make something similar to this page https://www.traductorbinario.com/#ascii , but with an integer input. This is what i have at the moment:
%include "io.mac"
.DATA
msg_binary db "Text in binary",0
.UDATA
number resd 1
.CODE
.STARTUP
GetLInt [number]
mov EAX, [number]
mov EBX, 10
len:
inc ECX
div EBX
xor EDX, EDX
cmp EAX, 0
jne len
PutStr msg_binary
nwln
mov EBX, ECX
sub ECX, ECX
mov ESI, number
repeat:
mov AL,[ESI]
mov AH,80H
mov ECX,8 ; loop count to print 8 bits
print_bit:
test AL,AH ; test does not modify AL
jz print_0 ; if tested bit is 0, print it
PutCh '1' ; otherwise, print 1
jmp skip1
print_0:
PutCh '0' ; print 0
skip1:
shr AH,1 ; right-shift mask bit to test
loop print_bit
PutCh ' '
inc ESI
dec EBX
cmp EBX, 0
jne repeat
nwln
.EXIT
I assume you are using Dandamudi's io.mac
on Linux:
Table B.1 Summary of I/O Routines Defined in io.mac
Name Operand(s) Operand location Size What it does PutCh source value 8 bits Displays the character register located at source memory GetCh destination register 8 bits Reads a character into memory destination nwln none — — Displays a carriage return and line feed PutStr source memory variable Displays the NULL-terminated string at source GetStr destination [,buffer size] memory variable Reads a carriage-return-terminated string into destination and stores it as a NULL-terminated string. Maximum string length is buffer size-1. PutInt source register 16 bits Displays the signed 16-bit number memory located at source GetInt destination register 16 bits Reads a signed 16-bit number into memory destination PutLint source register 32 bits Displays the signed 32-bit number memory located at source GetLint destination register 32 bits Reads a signed 32-bit number into memory destination (from: Dandamudi, Sivarama P., Introduction to Assembly Language Programming, 2nd ed. 2004)
For your purpose, you must first convert GetLInt
's 32-bit integer to a decimal ASCII string. Then you can convert the single characters into a binary string.
The following program uses a procedure to perform the first conversion. Note that Dandamudi's .EXIT
is not at the end of the source code, but where the program returns to the shell. Furthermore, I do not agree with Dandamudi's approach of linking the character to a variable mask using TEST
. It is easier to isolate one bit per SHL eight times.
%include "io.mac"
.DATA
msg_binary db "Text in binary",0
.UDATA
number resd 1
decimal resb 11 ; Max. ten digits plus one NULL
.CODE
.STARTUP
GetLInt [number] ; Input & convert input to 32-bit integer
PutStr msg_binary
nwln
mov eax, [number] ; Value of number
mov edi, decimal ; Offset of decimal
call int2str ; Convert number to decimal
mov esi, decimal ; Offset of decimal
repeat:
mov al, [esi] ; Load a character
cmp al, 0 ; End of string?
je done ; Yes -> end of repeart
mov ecx, 8 ; Loop eight times
loop1:
shl al, 1 ; Isolate the leftmost bit and shift the rest to the left
jnc print_0 ; if tested bit is 0, print it
PutCh '1' ; otherwise, print 1
jmp skip1
print_0:
PutCh '0' ; print 0
skip1:
loop loop1 ; Loop and decrement ECX
PutCh ' '
add esi, 1 ; Offset of next character
jmp repeat ; Once more
done:
nwln ; New line
.EXIT ; Return to shell
int2str: ; Converts an positive integer in EAX to a string pointed to by EDI
xor ecx, ecx
mov ebx, 10
.LL1: ; First loop: Collect the remainders
xor edx, edx ; Clear EDX for div
div ebx ; EDX:EAX/EBX -> EAX Remainder EDX
push dx ; Save remainder
inc ecx ; Increment push counter
test eax, eax ; Anything left to divide?
jnz .LL1 ; Yes: loop once more
.LL2: ; Second loop: Retrieve the remainders
pop dx ; In DL is the value
or dl, '0' ; To ASCII
mov [edi], dl ; Save it to the string
inc edi ; Increment the pointer to the string
loop .LL2 ; Loop ECX times
mov byte [edi], 0 ; Termination character
ret ; RET: EDI points to the terminating NULL
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.