flat assembler
Message board for the users of flat assembler.

Index > Main > Printing all elements of array

Author
Thread Post new topic Reply to topic
TmX



Joined: 02 Mar 2006
Posts: 841
Location: Jakarta, Indonesia
TmX 20 Apr 2012, 17:49
I want to print all the elements of array "1a2b3c4d"

Code:

format PE console 4.0

include 'win32a.inc'

entry start

section '.data' data readable
array db "1a2b3c4d",0
fmt db "%c",0

section '.code' readable executable
start:
   mov ecx, 1
  
mainLoop:
   cmp ecx, 7
  jne print 
  je okay
     
print: 
     inc ecx
     cinvoke printf, fmt, [array+eax]
    jmp mainLoop
        
okay:
       invoke ExitProcess, 0

section '.idata' data import readable writeable
library kernel32, 'kernel32.dll',  msvcrt, 'msvcrt.dll'
import kernel32, ExitProcess, 'ExitProcess'
import msvcrt, msvcrt, 'printf'
    


The result is:
Quote:

flat assembler version 1.70 (1701741 kilobytes memory)
arraytest.asm [22]:
cinvoke printf, fmt, [array+eax]
C:\FASM\INCLUDE/macro/proc32.inc [41] cinvoke [4]:
pushd arg
error: invalid size of operand.


Any idea?
Post 20 Apr 2012, 17:49
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 20 Apr 2012, 18:07
This works for me:
Code:

format PE console 4.0 

include 'win32a.inc' 

entry start 

section '.data' data readable 
array     db "1a2b3c4d",0 
fmt       db "%c",0 

section '.code' readable executable 
start: 
        mov        ecx, 0 
         
mainLoop: 
        cmp   ecx, 8
        jne   print  
        je   okay 
         
print: 
        lea        eax, [ecx + array]
        push      ecx
        cinvoke  printf, fmt, [eax]
        pop       ecx
        
        inc  ecx
        jmp      mainLoop 
         
okay: 
        invoke  ExitProcess, 0 

section '.idata' data import readable writeable 
library kernel32, 'kernel32.dll',  msvcrt, 'msvcrt.dll' 
import kernel32, ExitProcess, 'ExitProcess' 
import msvcrt, printf, 'printf'     
Post 20 Apr 2012, 18:07
View user's profile Send private message Visit poster's website Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 841
Location: Jakarta, Indonesia
TmX 20 Apr 2012, 19:10
Ah I see. "lea eax [ecx + array]" means to load N-th element of the array into eax.

But why push ecx and pop ecx are also needed?
Post 20 Apr 2012, 19:10
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 20 Apr 2012, 19:14
TmX wrote:
But why push ecx and pop ecx are also needed?
Some API functions from standard libraries, like 'printf' in this case, destroy the value of ecx register, so we need to save its copy somewhere (on the stack, for example).
Post 20 Apr 2012, 19:14
View user's profile Send private message Visit poster's website Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 676
Location: Mar del Plata
Enko 20 Apr 2012, 20:12
In WinApi; esi, edi and ebx are the only register preserved. Other may and certenly will be modified by the function called in the api.

in your case, is better to use one of the preserved registers, so you don´t need to make the push pop.
Post 20 Apr 2012, 20:12
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 20 Apr 2012, 20:16
Yes, that's true. I just didn't want to change TmX's source too much (for clarity) and made only most important corrections. Wink
Post 20 Apr 2012, 20:16
View user's profile Send private message Visit poster's website Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 841
Location: Jakarta, Indonesia
TmX 22 Apr 2012, 02:58
Enko wrote:
In WinApi; esi, edi and ebx are the only register preserved. Other may and certenly will be modified by the function called in the api.

in your case, is better to use one of the preserved registers, so you don´t need to make the push pop.


Ah yes, I totally forget about this part. Embarassed
Thanks.
Post 22 Apr 2012, 02:58
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.