1 Replies - 221 Views - Last Post: 23 April 2014 - 09:36 PM

#1 Biowin92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-May 13

Nested loop crash - Assembly

Posted 23 April 2014 - 08:40 PM

I've been working on this program for a while and I've hit a point where I don't know what is causing it to crash. It assembles fine, but it seems right when it hits the Innerloop it'll crash. The program is supposed to have one student open all the lockers, and the second student will open/close every second one and so on until all squared numbers are open. I think my problem may lie with Xor, but I am not entirely too sure, because even if I change it to use the mov instruction with conditioned jumps, it'll still crash. The Array is set to 101 because we're supposed to work with 1-100 not 0-99.

Assembly x86, ASM, and I am using Microsoft Visual Studios 2010.

.data
Locker Byte 101 dup(0)
temp Dword 1
Students DWORD ?
.code
main Proc
mov esi, OFFSET Locker
mov ecx, SIZEOF Locker
dec ecx
mov eax, 0
OuterLoop:
mov Students, ecx; keeps counter for Outer
mov ecx, 100; The lockers
inc eax
Call WriteDec; to show which locker is opened/closed

InnerLoop:
xor Locker[esi], 1
add esi, temp; goes to next locker

Loop InnerLoop

mov ecx, Students
cmp Locker[esi], 0
jz isClosed
jnz isOpen

isClosed:
mWrite" closed."
Call Crlf
jmp Restart

isOpen:
Mwrite" open."
Call Crlf
jmp Restart

Restart:
inc temp
loop OuterLoop
main ENDP



Is This A Good Question/Topic? 0
  • +

Replies To: Nested loop crash - Assembly

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,282
  • Joined: 28-March 11

Re: Nested loop crash - Assembly

Posted 23 April 2014 - 09:36 PM

Here:
mov esi, OFFSET Locker

You are moving the address off Locker into esi.

Here:
xor Locker[esi], 1

You are using the value in esi (the address of Locker) as an index into Locker. This is definitely wrong and way, way out of the array bounds. You will also get a crash at the end since there is not a ret.

Let's use edi as the index into Lockers:

.data?
Students DWORD ?
Locker Byte 102 dup(0)

.code
main Proc
	mov     ecx, SIZEOF Locker
	dec     ecx
	
	mov     eax, 0
OuterLoop:
	mov     Students, ecx; keeps counter for Outer
	inc     eax
	Call    WriteDec; to show which locker is opened/closed
	mov     edi, 0
InnerLoop:	
	xor     Locker[edi], 1	
	add     edi, 1 ; goes to next locker
	Loop    InnerLoop
	
	mov     ecx, Students 

	cmp     Locker[edi], 0
	jz      isClosed
	jnz     isOpen

isClosed:
	mWrite" closed."
	Call    Crlf
	jmp     Restart

isOpen:
	mWrite" open."
	Call    Crlf
	jmp     Restart

Restart:
	loop    OuterLoop
	ret
main ENDP
end main


Now the loops work and it prints out open, closed etc. Now modify to fit your needs.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1