0

Hello I'm doing a simple program in FASM where I set a dynamic array and I calculate the sum or other things. I need to allocate the memory for the array, how can I do that?

Like I want that "esi" points to a allocated memory to store the array data, so next I can do [esi+n] to save and get the values in the array.

format PE console
entry start

include 'include/WIN32A.INC'

; ====================================================================================
section '.data' data readable writeable

    insert_array_len    db  "Insert Array Size: ",0
    insert_val_str   db  "Insert Value: ",0

; ====================================================================================
section '.text' code readable executable

start:
    
    push ebp
    mov ebp,esp

    xor edi,edi

    mov esi,insert_array_len
    call print_str
    call read_hex
    mov edx,eax
    mov ecx,eax
    jz finish

set_values:
    mov esi,insert_val_str
    call print_str
    call read_hex

    mov [ebp+8+edi], eax
    inc edi
    loop set_values
    
    mov ecx,edx
    xor edi,edi

print_values:
    mov eax,[ebp+8+edi]
    inc edi
    call print_eax
    loop print_values

finish:
    call [ExitProcess]


include 'training.inc'

print_eax inside "training.inc":

; ================[print_eax]====================
; Prints eax to console:

print_eax:
    pushad      ; Keep all registers.

; Skip over the data:
    jmp     .print_eax_after_data
    .print_eax_fmt   db          "%x",10,13,0
.print_eax_after_data:

    push    eax     ; The argument.
    push    .print_eax_fmt
    call    [printf]
    add     esp,8
    popad           ; Restore all registers.
    ret
Davide
  • 73
  • 6
  • 1
    Invoke dynamic memory allocation function of your OS. Alternatively, use the stack. See also this [question](https://stackoverflow.com/q/9006408/547981) – Jester Aug 05 '22 at 22:08
  • @Jester I have updated the code, I'm trying to use the stack but I get a memory access error "Unhandled exception: page fault on read access to 0x00001010 in 32-bit code (0x0000101". I'm sure I'm doing something really wrong xD – Davide Aug 05 '22 at 22:13
  • 1
    When you single-step, what path of execution leads to that error? Is it after a `ret` to a bad address or something, or is it inside `call print_str`? Does it really take a pointer in ESI, not following any standard 32-bit calling convention? Also, your code doesn't `sub esp, 32` or anything like that to reserve stack space, and `mov [ebp+edi], eax` is doing a 4-byte store to some where relative to the frame pointer (which points at the saved EBP right below your return address.) – Peter Cordes Aug 05 '22 at 22:17
  • @PeterCordes I think I was forgetting to add "8" to ebp to write in the stack after the return address because now I'm adding 8 to ebp when setting the data and the code works a little more xD – Davide Aug 05 '22 at 22:19
  • With the code I have updated now I get the following output: "Insert Array Size: 2 Insert Value: 5 Insert Value: 20 2005 20 zsh: segmentation fault wine 7_sum_array.exe". I don't know why but it print 2005 instead of 5 and it has a segmentation fault :/ – Davide Aug 05 '22 at 22:20
  • 1
    After setting up EBP as a frame pointer, `[ebp+8]` is the first stack arg for your function. Normally you need to reserve new space *below* your return address, like you're doing with `push` (which does ESP-=4 as well as a store). It might happen to work in `start` to scribble over your caller's stack-frame, especially if you just call exit instead of returning, but don't expect that in a normal function. Look at compiler output (https://godbolt.org/) for some simple C functions that use local arrays, although note that GCC's intel-syntax output is MASM-like. – Peter Cordes Aug 05 '22 at 22:21
  • @PeterCordes You are right, now I'm calling exit and I don't get any segmentation fault. But it still print all the data with the first call to "print_eax" in the "print_values" part. I mean if I populate the array with the values 50 and 100 the first output I get from "call print_eax" after doing "mov eax,[ebp+8+edi]" is 10050 instead of 50. The second output is correct (so is just 100) Why it also get the second value when I print the first time? (I have updated the code) – Davide Aug 05 '22 at 22:26
  • 1
    Well yeah, overlapping 4-byte loads are going to get non-zero bytes in the upper 3 of EAX. You're only incrementing EDI by 1, not 4. Single step with a debugger and look at memory if you want to see what's happening. If you want to do a zero-extending byte load into 32-bit EAX, use `movzx eax, byte [...]` – Peter Cordes Aug 05 '22 at 22:33
  • Thank you it works now! I was trying with "mov eax, byte ptr [ebp+8+edi]" but I was getting an error. With movzx it works correctly now! Thank you! – Davide Aug 05 '22 at 22:36
  • I have the same problem with the addition. When I try to do: add eax,[ebp+8+edi] if I have an array = [10,20] instead of getting 30 I am getting "2010". Why is that? I mean add shouldn't just add the value of eax? Instead it write "20" in the high bits of eax – Davide Aug 05 '22 at 23:00

0 Answers0