flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
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 ![]()
|
|||||||||||
![]() |
|
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' |
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.