Author Topic: Reading array with for loop  (Read 19098 times)

Offline yvz

  • Jr. Member
  • *
  • Posts: 5
Reading array with for loop
« on: January 11, 2014, 02:36:28 PM »
Here I'm with another newbie question. Ok I actually want to do is using arrays in for loops to manipulate or copy/move when it's needed. As a start I wanted to read a hard coded array with for loop. Here's my code
Code: [Select]
section .data

array1: dd 15, 13, 10, 5, 6, 3, 3, 7, 12, 9
array1Len : equ $-array1
range : equ 16

section .bss
array2 resb 16
array3 resb 10

section .text
global _start

_start:
mov eax, 0
jmp for1
;jmp for2
;jmp for3
;jmp for4

for1:
cmp eax, array1Len ;for loop control word
je finished ; if equal jump to finished
push array1 ; pushing array to stack
push eax ; pushing eax to stack for later use
mov eax, 4 ; system call for write
mov ebx, 1
add esp, 1 ; with this I meant to point arrays first element at stack
pop ecx ; first element to ecx
int 80h

dec esp ; pointing pushed loop controller again
pop eax ; move index to eax
add eax, 1 ; add index 1 for each loop
jmp for1

finished:
mov eax, 1
mov ebx, 0
int 80h

Yet when I run this, which gives no error, it seems that there's an infinite loop situation appears. Program neither stop nor give any result for some reason. Any help or suggestions would be appreciated

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Re: Reading array with for loop
« Reply #1 on: January 11, 2014, 02:50:30 PM »
Hi!

suggestion1:

This might be the basic loop:
Code: [Select]
section .data

      array1: dd 15, 13, 10, 5, 6, 3, 3, 7, 12, 9
      array1Len : equ $-array1
      range : equ 16

section .bss
      array2 resb 16
      array3 resb 10

section .text
global _start

_start:


      mov ecx, array1Len      ; Set counter
for1:

      push ecx                ; Save counter



      pop ecx                 ; Restore counter
      dec ecx                 ; Update counter
      jnz for1

finished:
      mov eax, 1
      mov ebx, 0
      int 80h

suggestion2:

It will push array1 address, not whole array.
Code: [Select]
push array1 ; pushing array to stack

EDIT0:

suggestion3:

Here is Linux System Calls

You system call 4:
Code: [Select]
mov eax, 4 ; system call for write
mov ebx, 1
add esp, 1 ; with this I meant to point arrays first element at stack
pop ecx ; first element to ecx
int 80h

is missing edx.

:)

EDIT1:

suggestion4:

Code: [Select]
mov edx,4 ; message length
mov ecx,msg ; message to write
mov ebx,1 ; file descriptor (stdout)
mov eax,4 ; system call number (sys_write)
int 0x80 ; call kernel

;http://www.tutorialspoint.com/assembly_programming/assembly_system_calls.htm

Things to think about: Your array is full of 32bit numbers, can, linux system call 4, display them?
« Last Edit: January 11, 2014, 03:18:38 PM by encryptor256 »
Encryptor256's Investigation \ Research Department.

Offline yvz

  • Jr. Member
  • *
  • Posts: 5
Re: Reading array with for loop
« Reply #2 on: January 11, 2014, 04:04:50 PM »
Thank you encryptor, I'm working on it right now but your suggestions help a lot. I'll reply here what I got.

Offline yvz

  • Jr. Member
  • *
  • Posts: 5
Re: Reading array with for loop
« Reply #3 on: January 16, 2014, 11:41:55 PM »
Mostly thanks to this forum I managed to make it work. I implemented a Counting Sort algorithm for NASM. But for the love of sanity please suggest noobies GDB at first :) It's a total life saver.

Anyways here's the code. It works well, but because printing 2 digit numbers are a little out of my interest for now, I didn't spend time for that.

Code: [Select]
;nasm -f elf count.asm
;ld -s -o count count.o
;./count


%define STEP_SIZE 4
%define RANGE 10
%define LENGTH 15
%define STDOUT 1

section .data
global array1
array1: dd 3, 4, 1, 1, 7, 5, 3, 0, 9, 2, 1, 3, 4, 5, 5
arrayLen : equ   LENGTH
array2: times RANGE dd 0
tmp: dd 0
spc: db ' - '
spcLen: equ $-spc
spc2: db ' - '
spc2Len: equ $-spc2

section .bss
result resd 50
section .text
global _start

_start:
call count
mov eax, 0
push ecx
mov ecx, result
call display
pop ecx
mov eax, 1
int 80h

count:
pushad
mov ebx, array1
mov ecx, array2
mov eax, arrayLen
call for1
mov ecx, array2
call for2
mov eax, arrayLen
mov ebx, array1
call for3
popad
ret

for1:
push eax
mov eax, [ebx]
add ebx, STEP_SIZE
add dword [ecx + STEP_SIZE*eax], 1
mov eax, [ecx + STEP_SIZE*eax]
pop eax
sub dword eax, 1
cmp eax, 0
jnz for1
ret

for2:
push eax
mov dx, 0 ;for sum
add dx, [ecx + eax*STEP_SIZE]
inc eax
add dx, [ecx + eax*STEP_SIZE]
mov [ecx + eax*STEP_SIZE], dx
mov eax, [ecx + eax*STEP_SIZE]
pop eax
add eax, 1
cmp eax, RANGE-1
jne for2
ret

for3:
push eax
mov dx, 0 ;result value
sub dword eax, 1
push eax
mov eax, [ebx + eax*STEP_SIZE] ;get first array's eax value
sub dword [ecx + eax*STEP_SIZE], 1 ;mid array - 1
mov eax, [ecx + eax*STEP_SIZE] ;put new value to eax as index
pop edx
mov dx, [ebx + STEP_SIZE*edx] ;put that value at array to dx
mov [result + eax*STEP_SIZE], dx ;move dx to result array at given point
mov eax, [result + eax*STEP_SIZE]
pop eax
sub dword eax, 1
cmp eax, 0
jnz for3
ret
 
display: ;array print (Version4)
push eax ;save counter
push ecx
mov ecx, spc2
mov edx, spc2Len
mov ebx, STDOUT
mov eax, 4
int 80h

pop ecx
pop eax
push eax
mov eax, [ecx + STEP_SIZE *eax]
add eax, '0' ; convert to ascii
mov [tmp], eax
mov ebx, STDOUT
push ecx
mov ecx, tmp
mov edx, STEP_SIZE
mov eax,4
int 80h

mov ecx, spc
mov edx, spcLen
mov ebx, STDOUT
mov eax, 4
int 80h

pop ecx
pop eax
inc eax
cmp eax, arrayLen
jne display
ret
« Last Edit: January 17, 2014, 10:07:14 PM by yvz »

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Re: Reading array with for loop
« Reply #4 on: January 18, 2014, 06:40:29 PM »
Cool! That's nice, that you managed to solve your problem!

Mostly thanks to this forum I managed to make it work. I implemented a Counting Sort algorithm for NASM. But for the love of sanity please suggest noobies GDB at first :) It's a total life saver.

Well, it's seems there is a thing that i don't know, what is GDB? :)

Encryptor256's Investigation \ Research Department.

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Reading array with for loop
« Reply #5 on: January 18, 2014, 06:50:51 PM »
Well, it's seems there is a thing that i don't know, what is GDB? :)

GNU/Linux debugger

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Re: Reading array with for loop
« Reply #6 on: January 18, 2014, 07:03:25 PM »
Encryptor256's Investigation \ Research Department.