4 Replies - 13440 Views - Last Post: 27 February 2013 - 05:55 PM

#1 Vam521   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-February 13

Assembly Language, Reverse an Array, Error with code

Posted 26 February 2013 - 09:26 PM

Hey, so this is what I have done so far, but my code does not work. We are supposed to make a code that accept 10 user inputs, then puts them back out in reverse order. I have the loop working to print the numbers in the same order they were pushed into, I know this by commenting out the "call ReverseArray" I can see my program work, but once I un-comment it, the code does not work. I am supposed to use SIZEOF, TYPE, and LENGTHOF operators to make the program as flexible as possible to support the array size and type being changed in the future, but I am not fully there yet. Any help would be much appreciated. I am new to Assembly Language and finding it rather challanging.

INCLUDE Irvine32.inc ;// include the Irvine code for WriteInt
.data
	arr DWORD 10 DUP(?)					;// create an array that holds 10 elements supplied by user
	num WORD ?
	msg BYTE "Please enter # 10 numbers",0dh,0ah,0
	newLine BYTE 0dh,0ah,0
	msg2 BYTE "Here are the numbers printed backwards",0dh,0ah,0



.code

main PROC
	call Clrscr  

	mov esi, OFFSET arr					;//
	mov ecx, 10							;//counter (# of items in array to loop through)

	call FillArray
	call ReverseArray
	call PrintArray

	exit

main ENDP

FillArray PROC
	push esi
	push ecx
	mov edx, OFFSET msg
	call WriteString
	myLoop1:
		call ReadInt
		mov [esi], eax
		add esi, TYPE DWORD
	loop myLoop1
	pop ecx
	pop esi
	ret
FillArray ENDP


ReverseArray PROC
	
	myLoop1:
		pop esi
		pop ecx
		mov eax, arr[esi]
		add eax, TYPE DWORD
		call WriteInt
		
	Loop myLoop1
	push esi
	push ecx
ReverseArray ENDP

PrintArray	PROC

	push esi
	push ecx
	mov edx, OFFSET msg2
		call WriteString
	myLoop1:
		mov eax, [esi]
		call WriteInt
		add esi, TYPE DWORD
		call EndLine
	loop myLoop1

	pop ecx
	pop esi
	ret
PrintArray	ENDP

EndLine PROC
	push edx
	mov edx, OFFSET newline
	call WriteString
	pop edx
	ret
EndLine ENDP

;//procedure to fill loop based on user input
;//procedure to reverse the loop
;//procedure to print loop
;//endl procedure to put new line
	push esi
	push ecx

	pop ecx
	pop esi


END main



Is This A Good Question/Topic? 0
  • +

Replies To: Assembly Language, Reverse an Array, Error with code

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6794
  • View blog
  • Posts: 31,346
  • Joined: 10-May 07

Re: Assembly Language, Reverse an Array, Error with code

Posted 26 February 2013 - 09:31 PM

** Moved to assembly **
Was This Post Helpful? 0
  • +
  • -

#3 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Assembly Language, Reverse an Array, Error with code

Posted 26 February 2013 - 10:13 PM

Quote

but my code does not work

Yes and what does not work?????? I ran it, it crashed, I ran it through a debugger and found the problem.

This:
arr DWORD 10 DUP(?)

Does not hold 10 array elements but 11. Indexes start at 0 NOT 1!!!

To hold 10 elements the above should be:
arr DWORD 9 DUP(?)


Assembly is case sensitive. You declared newLine, but use newline in the code.

You do not need all those push/pops, irvine saves ALL registers at the beginning of his procs.

We will get rid of reversearray and use PrintArray instead, printing the array from the last element.

.data
arr         DWORD 9 DUP(?)					;// create an array that holds 10 elements supplied by user
arr_len     equ ($ - arr) / 4           ; array size
msg         BYTE "Please enter # 10 numbers",0dh,0ah,0
newLine     BYTE 0dh,0ah,0
msg2        BYTE "Here are the numbers printed backwards",0dh,0ah,0

.code
main PROC
	mov     esi, OFFSET arr					;//
	mov     ecx, 10							;//counter (# of items in array to loop through)

	call    FillArray
	call    PrintArray
    call    WaitMsg
	exit

main ENDP

FillArray PROC uses esi ecx
	mov     edx, OFFSET msg
	call    WriteString
	
myLoop1:
	call    ReadInt		
	mov     [esi], eax
	add     esi, TYPE DWORD
	loop    myLoop1
	ret
FillArray ENDP

PrintArray	PROC
	mov     edx, OFFSET msg2
	call    WriteString
	
	mov     edi, arr_len        ; array elements, start at last one.
myLoop1:
	mov     eax, [esi + edi * 4]; array pointer + index * 4 (size of a dword)
	call    WriteDec
	
	sub     edi, 1              ; decrease index
	call    EndLine
    loop    myLoop1
	ret
PrintArray	ENDP

EndLine PROC
	mov     edx, OFFSET newLine
	call    WriteString
	ret
EndLine ENDP
END main

Was This Post Helpful? 1
  • +
  • -

#4 Vam521   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-February 13

Re: Assembly Language, Reverse an Array, Error with code

Posted 27 February 2013 - 12:25 AM

I have a few questions.
Where you used,
 call WaitMsg 

What does this do?

Where
 call WriteDec 

was used what does this do?

And if possible can this part be explained a little better?
 equ ($ - arr) / 4  


I appreciate the help and apologize for all the questions, but I want to be able to understand what is going on. Thank you!
Was This Post Helpful? 0
  • +
  • -

#5 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Assembly Language, Reverse an Array, Error with code

Posted 27 February 2013 - 05:55 PM

Your using the Irvine library right? You have access to all of the code all of the functions in the library uses. If you don't have the source, download from irvines web page and open irvine32.asm to see what those functions do. The source is even commented!

WriteDec prints out only positive numbers, WriteInt prints out positive and negative numbers with a + or - in front of the number.

WaitMsg keeps the console open with the default text "Press any key to continue", and closes the console when you press a key.

I will leave it up to you to do some research as to what equ means and does.

As for the dollar sign, it is used to find the length of the the data pointed to by a label. (There are more advanced things you can do with $, but that is not important.)

Hello       db  "Hello there chap!",
Hello_len   equ $ - Hello

When you run your program, Hello_len will contain 17.

Hello       db  "Hello there chap!"
Hello2      db  "Don't say hello to me!"
Hello_len   equ $ - Hello

Hello_len will contain 39

arr         DWORD 9 DUP(?)     
arr_len     equ ($ - arr) / 4  

Here, arr_len will contain 10. Since the array made up of DWORDS, we divide the size by 4. When you need to use the size of something in code, it is easier to do it this way, as if you change the size of a variable, the len equ will always be correct.

In english, you are telling the Assembler/linker to take the address of the label (arr_len) before $ and subtract the address of the label (arr) after $ and you will get the length of the data in label (arr)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1