#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    NASM assmebly language, addition of 2 arrays, and store into 3rd


    my program needs some tweaking,

    so far it reads in 6 values then stores them into array1, then reads in another 6 and stores them into array 2

    then i initialiaze array3 to store the sums of array1 and array2,

    but i get a segmentation fault in my add function i believe

    any suggestions guys?
    I know my esp+XX values are off, but i've gone through every number up to 100, and none of them hold any values from array 1 and array2?
    ideas?


    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
    	pushad
    ;Declare Array1
    	push	 6		;declares max size of array1
    	push	 0		;indicates number of elements in array1
    	push	 array1		;declares starting address of array1
    	
    	mov	ecx, 0
    	mov 	eax, prompt1	
    	call	print_string
    	call	print_nl
    
    	call	read_sarray32	;begin reading values into array
    	call	print_sarray32	;print out values of array1
    
    	pop	edx		;popped to keep balanced stack
    	pop	edx		;popped to keep balanced stack
    	pop	edx		;popped to keep balanced stack
    	popad
    	
    
    ;Declare Array2
    	pusha
    	push	6		;declares max size of array2
    	push	0		;indicates number of elements in array2
    	push	array2		;declares starting address of array2
    	mov	eax, prompt1
    	call	print_string
    	call	print_nl
    	
    	call	read_sarray32
    	call	print_sarray32
    	
    	pop	edx		;popped to keep balanced stack
    	pop	edx		;popped to keep balanced stack
    	pop	edx		;popped to keep balanced stack
    	popad
    	
    
    ;Declare Array3
    	pushad
    	push	6
    	;push	0
    
    	push	array2
    	push	array1
    	push	array3
    	call	add_arrays
    
    	call	print_sarray32
    	mov	eax, dprompt	;print out Goodbye Message
    	call	print_string
    	pop	ebx		;popped to keep balanced stack
    	pop	edx		;popped to keep balanced stack
    	pop	ecx		;popped to keep balanced stack
    	pop	eax		;popped to keep balanced stack
    	;pop	edx		;popped to keep balanced stack
    	popad
    	leave	
    	ret
    read_sarray32:
    	;enter 0,0
    	pushad
    	;mov	ebx, 0		;initialize ebx at 0
    	mov	ebx, [esp+36]	;array starting position
    	mov	edx, [esp+44]	;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	ebx, 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+40], ecx	;moves counter value back to stack	
    	popad
    	;leave
    	ret
    print_sarray32:
    	enter	0,0
    	pushad
    	mov	ecx, 0		;initialize counter to 0
    	
    	mov	ebx, [esp+40]	;move starting location to ebx
    	mov	edx, [esp+44]	;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, edx	;	edx
    	jz	print_sarrayDone
    	jmp	print_loop
    print_sarrayDone:
    	popad
    	leave	
    	ret
    add_arrays:
    	enter 0,0
    	pushad
    	
    	mov	ecx, 0		;initialize counter to 0
    	
    	mov	ebx, [esp+52]	;moves starting location of array1 into ebx
    	mov	ebp, [esp+54]	;moves starting locatino of array2 into ebp
    	mov	esi, [esp+52]	;moves starting loaction of array3 into esi
    	mov	eax, ebx
    	call	print_int
    	call	print_nl
    	
    	mov	eax, ebp
    	call	print_int
    	call	print_nl
    add_loop:
    	inc	ecx
    	
    	mov	eax, [ebx]	;moves value of ebx into eax
    	add	eax,[edx]	;moves value of ebp into edx	
    	mov	[esi], eax	;move he sum of values stored in eax into esi
    	
    	add	esi, 4		;increment location of pointer
    	add	ebx, 4		;increment location of pointer
    	add	ebp, 4		;increment location of pointer
    	
    	cmp	ecx, edx
    	jz	add_done
    	jmp	add_loop
    add_done:
    	popad
    	leave
    	ret
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    928
    Usually you save the value in ESP at the beginning of your function call into EBP and then use EBP to reference the location of your arguments (saving you the trouble of figuring out how far your register saving and local variables move the stack pointer).

    Code:
    somefunction:
        push ebp
        mov ebp esp
        ; save space for locals
        ; push other registers that are used locally
        ; do some computation
        ; restore registers
        mov esp ebp
        pop ebp
        ret
    Which leaves your memory in this layout inside your function:
    Code:
    +-----------+
    |           |
    | Caller's  |
    |           |
    | Registers |
    |           |
    +-----------+
    | Local Var | <-- EBP - 4
    +-----------+
    | Saved EBP | <-- EBP Points here
    +-----------+
    | Ret Addr  | <-- EBP + 4
    +-----------+
    | Argument1 | <-- EBP + 8
    +-----------+
    | Argument2 | <-- EBP + 12
    +-----------+
    |   . . .   |
    If you must do it via an offset from ESP, these are the questions you need to answer:
    (1) What does PUSHAD place onto the stack?
    (2) How much memory does this take up
    (3) Where does ESP point at the start of the function?
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0
    thanks for the reply

    I figured out the memory addreses of the 3 arrays i pushed,

    and i think it's adding correctly

    but now when i go to print out array3, it's either giving me garbage, or it's giving me values of array2... i tried finding the location of array3 and can't figure it out...

    also tried calling print inside the add function and no luck...

    also you said to move the esp value into ebx, and i think i did that when i moved the 3 addresses into 3 different registers....if that's what you meant?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    928
    Originally Posted by ilinan87
    but now when i go to print out array3, it's either giving me garbage, or it's giving me values of array2... i tried finding the location of array3 and can't figure it out...
    Post your current code.

    Originally Posted by ilinan97
    also you said to move the esp value into ebx, and i think i did that when i moved the 3 addresses into 3 different registers....if that's what you meant?
    No. I meant you use EBP (not the same as EBX) to store the address of the top of the stack (not a value stored on the stack) when your function starts. You then retrieve your arguments using an offset from EBP (which will stay constant regardless of what you push onto the stack later in the function).
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

IMN logo majestic logo threadwatch logo seochat tools logo