CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Other Programming > Assembly
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Assembly Questions and Answers for Assembly here!

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old February 27th, 2005, 04:11 PM
    utcguy utcguy is offline
    Junior Member
     
    Join Date: Feb 2005
    Posts: 4
    utcguy is an unknown quantity at this point (<10)
    Exclamation Array Help

    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.

    Code:
    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.

    Code:
    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
    Reply With Quote
      #2    
    Old February 27th, 2005, 06:23 PM
    Hobson's Avatar
    Hobson Hobson is offline
    Senior Member
     
    Join Date: Dec 2004
    Location: Poland
    Posts: 1,163
    Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)
    Re: Array Help

    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
    __________________
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

    Last edited by Hobson; February 27th, 2005 at 07:05 PM.
    Reply With Quote
      #3    
    Old February 27th, 2005, 07:07 PM
    svenhag's Avatar
    svenhag svenhag is offline
    Member
     
    Join Date: Jan 2005
    Location: Gothenburg, Sweden
    Posts: 134
    svenhag is on a distinguished road (20+)
    Re: Array Help

    Here's a nasm example

    Code:
    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
    __________________
    If you try and take a cat apart to see how it works,
    the first thing you'll have on your hands is a nonworking cat
    Reply With Quote
      #4    
    Old February 27th, 2005, 07:36 PM
    utcguy utcguy is offline
    Junior Member
     
    Join Date: Feb 2005
    Posts: 4
    utcguy is an unknown quantity at this point (<10)
    Re: Array Help

    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?
    Reply With Quote
      #5    
    Old February 28th, 2005, 05:37 AM
    Hobson's Avatar
    Hobson Hobson is offline
    Senior Member
     
    Join Date: Dec 2004
    Location: Poland
    Posts: 1,163
    Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)Hobson has much to be proud of (1500+)
    Re: Array Help

    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
    __________________
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Other Programming > Assembly


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 05:07 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009