Click to See Complete Forum and Search --> : Array Help


utcguy
February 27th, 2005, 04:11 PM
I am trying to get this program to work but all i get is a blank result and would like to know what I am doing wrong. Can you please point out what is wrong with it? Any help would be greatly appreciated.


i have created these subroutines to find the min & max elements in an array. i am not sure if it is correct.


include pcmac.inc
.model small
.586

.code


public amax

amax proc
push ebx
push ecx
mov eax, [ebx]; the first one

mx:
cmp eax, [ebx]; compare it with the current
jge cont ; its not the maximum
mov eax, [ebx] ; its a new maximum value

cont:
add ebx, 4 ; go to next element
dec ecx ; subtract 1 from count
jnz mx ; continue if its not zero
pop ecx
pop ebx
ret

amax endp


public amin

amin proc
push ebx
push ecx
mov eax, [ebx]; first one in array

mn:
cmp eax,[ebx]; compare with current min value
jle cont2; its not the minimum value
mov eax, [ebx]; its the new minimum value

cont2:
add ebx, 4 ; go to next element
dec ecx ; subtract 1 from count
jnz mn ; continue if its not zero
pop ecx
pop ebx
ret

amin endp
end



this is the main program that i created to utilize the subroutines, again I dont know if it is correct.


include pcmac.inc
.model small
.586
.stack 200h


.data

list dd -100, 7565, 380, -9866, 452, 1295, 626, -1128, 459
dd -762, 4722, 1369, 489, 8988, -23321, 6675, 4239
dd 5677, -999, 24863

lsize dd 20
arrmax dd ?
arrmin dd ?
maxmsg db 'Maximum element is: $'
minmsg db 'Minimum element is: $'

.code

extrn getddec:near, putddec:near
extrn amax:near, amin:near

lab6 proc
_begin


mov ebx, [list]
mov ecx, lsize
call amax
mov arrmax, eax
call amin
mov arrmin, eax

_putstr maxmsg
mov eax, arrmax
call putddec
_putch 13,10
_putstr minmsg
mov eax, arrmin
call putddec
_putch 13,10


lab6 endp
end lab6

Hobson
February 27th, 2005, 06:23 PM
Look here:
http://www.codeguru.com/forum/showthread.php?t=328170

Edit:
your searching routines work ok for signed numbers, I checked this.
In main program you need to change line
mov bx,[list] to mov bx,offset list
This may be the reaon of malfuncion in your program. I changed only this line and it worked. If thee is still something wrong, check printing macros.

Hob

svenhag
February 27th, 2005, 07:07 PM
Here's a nasm example


bits 32

global _start

array dd -100, 7565, 380, -9866, 452, 1295, 626, -1128, 459
dd -762, 4722, 1369, 489, 8988, -23321, 6675, 4239
dd 5677, -999, 24863

len dd 20

%define array_arg ebp+8
%define len_arg ebp+12

arr_max:
enter 0,0

xor eax, eax
mov ebx, [array_arg]
mov ecx, [len_arg]
dec ecx

for:
mov edx, [ebx+4*ecx]
cmp edx, eax
jle not_bigger
mov eax, edx

not_bigger:
loop for

leave
ret


_start:
push dword [len] ; push array length
push array ; push array address
call arr_max
add esp, 0x8

mov eax, 0x1 ; exit
int 0x80 ; make syscall

utcguy
February 27th, 2005, 07:36 PM
Thank you both. Hobson, i added the "offset" before and it worked. I am unsure as to why though. If its not too much trouble, can you give me an explanation as to why it is significant?

Hobson
February 28th, 2005, 05:37 AM
The main reason it worked is not adding word 'offset', but changing '[list]' to 'list'.
mov bx,[list] means: take a memory cell named list, look inside, and put its value in bx register,
mov bx, offset list means: take a memory cell named list, and put its address in bx register. In this case word 'offset' can be ommited with some assemblers. Check if yours require it or not. Word offset means, that we want to know, how far from the beginning of data segment our memory cell is located. With newer versions of assemblers use of this word is not necessary.

Hob