flat assembler
Message board for the users of flat assembler.

Index > Windows > Array Indexing

Author
Thread Post new topic Reply to topic
YourJeans



Joined: 25 Jan 2024
Posts: 1
YourJeans 25 Jan 2024, 12:27
I am a beginner to FASM and low-level programming and this is basically my first FASM program.

I am trying to create an array, and then print out it's values until I reach a value of 0. For example, in python it would look like this.

Code:
array = [1,2,3,4,5,0]

for i in array:
    if i == 0:
        break
    else:
        print(i)
    




However, I am having some problems doing this in FASM. The main problematic part of my code is as follows.

Code:
section '.data' data readable writable

    instruction dd 1,2,3,4,5,0
    formatInt dd "%d", 0

    indexcounter db 0

    endMsg dd "done", 0

section '.code' code readable writable

  start:

        main_loop:

           push ebp
           mov ebp, esp

           push dword [instruction+4*indexcounter]
           push formatInt

           call [printf]

           mov esp, ebp
           pop ebp

           call [getchar]


           inc dword [indexcounter]
           cmp dword [instruction+4*indexcounter], 0

           je end_loop

           jmp main_loop
    



When this program runs it just displays a blank command prompt that disappears after a few seconds. I've figured out that this is because of the way in which I am indexing the array, [instruction+4*indexcounter]. However, I don't know of an alternative way to do this. If someone could help that would be great. The code works if I just hardcode the indexes (e.g [instruction+4], [instruction+8]) However doing this would mean that I wouldn't be able to iterate over an array of any length.

As a side note, if you see any problems with my stack clearing, or how I am using printf, feel free to point them out.

I'll attach the entire program to the post as well.

I appreciate any suggestions Smile


Description:
Download
Filename: test.ASM
Filesize: 1.06 KB
Downloaded: 167 Time(s)

Post 25 Jan 2024, 12:27
View user's profile Send private message Reply with quote
fabbel



Joined: 30 Oct 2012
Posts: 92
fabbel 25 Jan 2024, 13:48
Hi
pbly cud be simplified / streamlined .... but quickly, it seems to me
1/
you are doing :
Code:
inc dword [indexcounter]
    

but indexcounter is declared as byte - not dword :
Code:
indexcounter db 0 
    

=> would change that to
Code:
indexcounter dd 0 
    


2/
indexcounter (in "[instruction+4*indexcounter]") ..
... is just a constant = address of corresponding variable (just like instruction is just an address)
... so [instruction+4*indexcounter] is actually always pointing to same fixed mem address
... would need to dereference indexcounter to read corresp. value

=> could try changing
Code:
push dword [instruction+4*indexcounter]
    

to
Code:
mov eax, dword [indexcounter]
push dword [instruction+4*eax]
    


3/
... same issue with
Code:
cmp dword [instruction+4*indexcounter], 0
    



... let others bring more comments to the table ...
Post 25 Jan 2024, 13:48
View user's profile Send private message Reply with quote
Hrstka



Joined: 05 May 2008
Posts: 63
Location: Czech republic
Hrstka 25 Jan 2024, 15:22
First you have to load indexcounter into a register, then you can use it as array index. Also the comparison if value is zero should be done before printf if you want to mimic the Python code. So the code should look like this.
Code:
format PE console

entry start

include 'win32ax.inc'

section '.data' data readable writable

    instruction  dd 1,2,3,4,5,0
    indexcounter dd 0

    formatInt db '%d', 0
    endMsg    db 'done', 0

section '.code' code readable writable

  start:

       main_loop:
           mov ecx, [indexcounter]
           mov eax, [instruction+4*ecx]
           cmp eax, 0
           je  end_loop

           inc ecx
           mov [indexcounter], ecx
           cinvoke printf,formatInt,eax
           cinvoke getchar

           jmp main_loop

        end_loop:
           cinvoke printf,endMsg
           invoke ExitProcess,0

section '.idata' import data readable

   library kernel, 'kernel32.dll',\
           msvcrt,'msvcrt.dll'

   import kernel,\
          ExitProcess, 'ExitProcess'

   import msvcrt,\
          printf, 'printf',\
          getchar, 'getchar'    
Post 25 Jan 2024, 15:22
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.