%include "asm_io.inc"
segment .data
msg1 db "Enter a number: ",0
msg2 db "Bad Number! try again. ",0
msg3 db "Enter Decimal value is: ",0
segment .bss
mask resd 1
segment .text
global main
main:
enter 0,0
pusha
mov dword[mask],1
mov esi,10
mov ebx,0
input:
mov eax,msg1
call print_string
call read_int
mov ecx,eax
next:
cmp eax,0
je cont
mov edx,0
div esi
cmp edx,1
ja error
jmp next
error:
mov eax,msg2
call print_string
call print_nl
jmp input
cont: mov eax,ecx
convert:
cmp eax,0
je sof
mov edx,0
div esi
cmp edx,0
je shift_1
or ebx,[mask]
shift_1:
shl dword[mask],1
jmp convert
sof:
mov eax,msg3
call print_string
mov eax,ebx
call print_int
call print_nl
popa
leave
ret
The program gets only 1 and 0 and prints its decimal value. It does not work on more than 10 bits number (i need it to be working on 32 bits number), also it does not work with number that starts with 0 (like 011) Help pls :)
I don't known what read_int
do exactly but if it is named accordingly it should read a 32 bit integer from input and return it in EAX
.
This means that you are requesting the user to input a binary number as a decimal numeral, in order to get a 32 bit number you need 32 decimal digits, so a number of the order of 10^32 or more or less 106 bit.
So this is clearly not the way to go. You need to read the binary digits one at a time, char by char. This way converting from the binary numeral to the number is even easier as you only need to do some shifting!
It also seems that what you want to do is just converting from the binary numeral to the number (ie user input binary number and you want its 32 bit value), since the print_int
function does that work of converting a number to a decimal numeral (ie printing a 32 bit value in decimal).
Here a sample program in NASM (to be linked with GCC) that do what you need. You only need the get_bin
function. This use C library functions but in a manner similar to your library routines. It also include a routine to print a 32 bit value in decimal, you may skip it.
Use the code in get_bin
as a reference.
BITS 32
GLOBAL _main
EXTERN _getchar
EXTERN _printf
SECTION .data
strInput db "Input a binary number: ", 0
strInputErr db "Binary number not valid!",0ah, 0
strChar db "%c", 0h
SECTION .text
_main:
push strInput
call _printf
add esp, 04h
call get_bin
jnc .input_error
push eax
call show_dec
.end:
ret
.input_error:
push strInputErr
call _printf
add esp, 04h
jmp .end
get_bin:
push ecx
push edx
mov ecx, 20h ;//32 bit
xor edx, edx ;Temp result
.get:
push edx
call _getchar
pop edx
cmp al, 0ah
je .return ;End early if user press Return
sub al, 30h ;ASCII digit -> digit value
cmp al, 01h ;Only 0 and 1 allowed
ja .end ;Here CF=0, meaning conversion failed
shr al, 01h ;Set the CF based on the new digit
rcl edx, 01h ;Shift this digit into EDX from the right
loop .get
.return:
mov eax, edx ;Return in EAX
stc ;CF=1 means conversion ok
.end:
pop edx
pop ecx
ret
;number
show_dec:
push ebp
mov ebp, esp
pushad
sub esp, 04h
mov BYTE [ebp-04h], 0
mov ebx, 1000000000d
mov eax, DWORD [ebp+08h]
mov ecx, 10d
.loop:
xor edx, edx
div ebx
test al, al
setnz ah
or BYTE [ebp-04h], ah
setnz ah
test cl, cl
setz ch
or ch, ah
jz .continue
add eax, 30h
pushad
push eax
push strChar
call _printf
add esp, 08h
popad
.continue:
xor ch, ch
mov eax, edx
push eax
mov eax, ebx
xor edx, edx
mov ebx, 10d
div ebx
mov ebx, eax
pop eax
loop .loop
add esp, 04h
popad
pop ebp
ret 04h
Please note that by default input from terminal/console is cooked.
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.