As title says
Just trying to read in values and store them in an array

Then call print function to print them out

As of now the program works, but only prints out the last value entered and the other outputs are 0's

here is the code...

%include "asm_io.asm"

segment .bss

array1      resd 6
array2      resd 6
array3      resd 6
segment .data
prompt1     db  "Enter your number(if done enter: 0 ) : ", 0
aprompt1    db  "Array1 Contents: ", 0
aprompt2    db  "Array2 Contents: ", 0
aprompt3    db  "Maximum Array Entries Reached: " , 0
dprompt     db  "Goodbye ! ", 0

segment .text
    extern puts, _printf, scanf, dump_line
    global asm_main

asm_main:
    enter   0,0
    pusha
;Declare Array1
    pusha
    push     6      ;declares max size of array1
    push     0      ;indicates number of elements in array1
    push     array1     ;declares starting address of array1
    mov     eax, prompt1    
    call    print_string
    call    print_nl

    call    read_sarray32
    call    print_sarray32

    mov eax, dprompt
    call    print_string
    pop edx
    pop edx
    pop edx
    popa
    leave   
    ret

;Declare Array2
    ;pusha
    ;push   6
    ;push   0
    ;push   array2
    ;mov    eax, prompt1
    ;call   print_string
;   
;   call    read_sarray32

;Declare Array3
;   pusha
;   push    6
;   push    0
;   push    array3
;   mov eax, prompt1
;   call    print_string
;   
;   call    ;to adding array1 and array2
;   popa
read_sarray32:
    enter 0,0
    pusha
    mov ebx, 0      ;initialize ebx at 0
    mov ebx, [esp+40]   ;array starting position
    mov edx, [esp+48]   ;array max size
    mov ecx, 0


read_loop:
    mov     eax, prompt1
    call    print_string
    call    read_int
    inc ecx ;increment counter
    cmp eax, 0  
    jz  Done
    jmp continue_loop
continue_loop:

    mov     [ebx], eax  ;move value into memory slot ebx
    add edx, 4      ;move to next location for db words

    cmp ecx, 6  ; did i reach maximum values of array size?
    jz  maximum_entries 
    jmp read_loop
maximum_entries:
    mov eax, aprompt3
    call    print_string
    jmp Done
Done:
    mov [esp+44], ecx   ;moves counter value back to stack  
    popa
    leave
    ret
print_sarray32:
    enter   0,0
    pusha
    mov ecx, 0      ;initialize counter to 0

    mov ebx, [esp+40]   ;move starting location to ebx
    mov edx, [esp+48]   ;move # items to edx

    mov eax, aprompt1
    call    print_string
    call    print_nl

print_loop:

    mov eax, [ebx]  ;move value of index 0 of array to eax
    call    print_int   ;print out number 
    call    print_nl
    add ebx, 4      ; moved ebx location to next slot
    inc ecx     ; counter is incremented by 1
    cmp ecx, 6
    jz  print_sarrayDone
    jmp print_loop
print_sarrayDone:
    popa
    leave   
    ret

Edited 3 Years Ago by mike_2000_17: Fixed formatting

The big problems I see right away are:
1. you are using 32 bit registers and using pusha, pusha saves the 16 bit registers.. you need to use pushad

2. your stack is really screwed up!! you are using pusha without using popa. for every pusha/d you need to use a corresponding popa/d. If not, you leave the stack unbalanced.

3. Why use pusha/d? It is a fairly slow instruction. You don't need to save ALL of the registers..

Fix the stack issue and we will go from there...

thanks for the reply, this is my second program in assembly so everything helps

i added the pushad and popads and kept it balanced

I'm assuming those fixes weren't meant to fix the issue of not displaying all numbers entered.

as I am still only printing out the last number entered

I believe I just made a breakthrough in this

in the label
continue_loop

i did
add ebx, 8

instead of the 4

and I"m printing out the digits I entered however it will print a number i entered, then a 0, then a number, then a 0

any ideas as to why it's added zeros between each number?

got my program to read and store the values entered into 2 different arrays with that code provided.

now I need to add both the contents of those two arrays and store them into a third array, how would i start that?

I have a very similar problem, could you post your updated code?
I read in the values, but then when it goes to print the array I get a segmentation fault

This article has been dead for over six months. Start a new discussion instead.