Author Topic: My first counter program  (Read 11006 times)

Offline qiet72

  • Jr. Member
  • *
  • Posts: 6
My first counter program
« on: September 15, 2010, 12:15:44 PM »
Hi,

I am attempting to create a counter program in assembler.  I would like to do something like this little C program:

Code: [Select]
/* Counter program */

#include <stdio.h>

main()
{
   long long int counter;
   counter = 1;
   for ( ; ; )
   {
   counter = counter + 1;
   printf("%015lld\n", counter);
   }
}

Here is my first attempt.  It, unfortunately, only works up to 9, then prints ascii characters instead of numbers:

Code: [Select]
; Count up and print each time until end

section .text
   global _start

_start:
   mov   eax,0    ; Initialize eax to 0

loop:
   inc   eax      ; Increment edx
   cmp   eax,50  ; Stop counting when we reach 50
   jz    exit     ; Exit the program
   push  eax      ; Push the non converted eax to the stack
   add   eax,'0'  ; Convert number to a string
   push  eax      ; Push the converted eax to the stack
   mov   ecx,esp  ; Get address of the current stack pointer into ecx for sys_write
   mov   eax,4    ; setup sys_write
   mov   ebx,1    ; stdout
   mov   edx,1    ; one character
   int   80h      ; call sys_write
   pop   eax      ; Release the converted item in the stack
   call  newline  ; Call newline function to print a newline
   pop   eax      ; Get the non converted eax back
   jmp   loop

newline:
   mov   edx, 10  ; Move 'newline' character into edx
   push  edx      ; Put it into the stack
   mov   ecx, esp ; Put the current stack pointer into ecx for sys_write
   mov   eax,4    ; sys_write
   mov   ebx,1    ; stdout
   mov   edx,1    ; Only on character to print out
   int   80h      ; call sys_write
   pop   edx      ; Don't need newline anymore, get rid of it so stack pointer points to what it was before
   ret

exit:
   mov   eax,1    ; Function exit
   mov   ebx,0    ; Return code 0
   int   80h      ; Execute function

I know that sys_write is using only one character and that it should find the length properly.

qiet72

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: My first counter program
« Reply #1 on: September 15, 2010, 12:58:33 PM »
Your next task is to create a small function that converts a number contained in a register to an ascii string. Create a .data or .bss section, define an array of bytes that will hold the converted value, and call your converter function with a value from within your main program loop.

You can also set up the stack to contain the buffer:
Code: [Select]
_start:
   sub  esp, 32   ; create a 32 byte buffer
   mov   eax,0    ; Initialize eax to 0
   .
   .
   mov  ecx, esp  ; point ecx to the buffer
   call bin2ascii ; convert value in eax to string
   .
   .
exit:
   add  esp, 32   ; restore stack
   mov   eax,1    ; Function exit
   mov   ebx,0    ; Return code 0
   int   80h      ; Execute function

To make your converter function generalized call it with 2 parameters: The number to convert and a pointer to the address of your buffer that will contain the output.  Finally you will call sys_write with the address of your buffer and the length of its contents.

There are many examples of such routines in other posts here if you search for them but try writing it yourself first ;)