Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

linux nasm assembly prints out random characters

$
0
0
I am writing a program to get an integer from the user, and then print out all the numbers from 0 up to the number. My code gets the input fine, but when printing it out, it prints continuously in what seems to be an endless loop. Here is my code:

SECTION .data ; Constant variable declaration
len EQU 32 ; Constant length
msg db "Enter a number: ", 0 ; Input message
msglen EQU $-msg ; Input message length

SECTION .bss ; Uninitialised data declaration
other resd len ; Output counter that is incremented
data resd len ; Input data buffer

SECTION .text ; Main program initialiser

GLOBAL _start ; Linker entry point declaration
_start: ; Entry point
nop ; This keeps the debugger happy :)

Msg: ; This section prints out the message
mov eax, 4 ; }
mov ebx, 1 ; }
mov ecx, msg ; } System_write call
mov edx, msglen ; }
int 80h ; }

input: ; This section gets the integer from the user
mov eax, 3 ; }
mov ebx, 0 ; }
mov ecx, data ; } System_read call
mov edx, len ; }
int 80h ; }

ASCIIAdj:
mov ebp, 48 ; This line sets the counter to '0' ASCII

setup: ; This section adjusts the counter
mov [other], ebp ; Increment counter

loop: ; This section loops, printing out from zero to the number given
mov eax, 4 ; }
mov ebx, 1 ; }
mov ecx, other ; } System_write call
mov edx, len ; }
int 80h ; }
mov eax, 1 ; Move 1 to eax
add ebp, eax ; Add eax to ebp (essentially increment ebp)
mov eax, other ; move other to eax
mov ebx, data ; move data to ebx
cmp eax, ebx ; compare them
jne setup ; If they are not the same, go back to the setup to increment other

exit: ; Exits the program
mov eax, 1 ; }
mov ebx, 0 ; } System_exit call
int 80h ; }

Expected output

Enter a number: 6
0123456

General Semantics of the program:

Display "Enter a number: "
Read in an integer less than 32 bytes in size.
Set a counter variable to the ASCII value of zero
Loop:
Display the character, adding 1 to it, and checking to see if it is equal to the value inputted.
If it is equal, goto the exit section and exit
Else loop.

Why does it loop continuously? I have incremented the counter, and compared the input and the counter, so why doesn't it break?

Thanks in advance

Viewing all articles
Browse latest Browse all 2703

Trending Articles