PDA

Click to See Complete Forum and Search --> : desperate help!!!


MrPolite
Nov 2nd, 2004, 11:28 PM
I've been working on this for 12 hours... I dont know why it doesnt work and my instructor hasnt been responding in the past 48hours... someone give me a little hint plz:cry: :cry: :cry:


INCLUDE irvine32.inc


.data
charArr BYTE 2 DUP(?)


.code
main PROC
; these just get two character inputs
call ReadChar
mov charArr, al
call ReadChar
mov [charArr+1], al


; push the characters onto the stack
push OFFSET [charArr + 1]
push OFFSET charArr
call swap



mov al, charArr
call WriteChar
mov al, [charArr+1]
call WriteChar
main ENDP



swap PROC
push ebp
mov ebp, esp



mov al, BYTE PTR [ebp+8]
mov bl, BYTE PTR [ebp+12]
mov [ebp+8], bl
mov [ebp+12], al


pop ebp
ret 8
swap ENDP


END main

readChar and writeChar are provided in libraries... just read and write chars. uuh it's supposed to swap the two characters stores in the charArr but it doesnt do anything. what's wrong

Peter Swinkels
Nov 18th, 2004, 05:00 AM
I don't know what assembler you are using but from what I can tell your program looks fine. Here is one suggestion though:
Perhaps you could use the XCHG (exchange (swap) instruction)?
Also what do you mean it does nothing? Does the pogram hang when executed or does it return the values that it supposed to swap without swapping them?

MrPolite
Nov 18th, 2004, 01:28 PM
hehe yeah it would display the result without them being swapped... I wrote this afterwards... after turning it in haha. it works:

INCLUDE irvine32.inc

.data
charArr BYTE 2 DUP(?)

.code
main PROC
; Get two character inputs and save them in a 2-byte array
call ReadChar
mov charArr, al
call ReadChar
mov [charArr+1], al


; push the offsets of characters onto the stack
mov eax, OFFSET charArr +1
push eax ; second parameter
dec eax
push eax ; first parameter

call swap2


mov al, charArr
call WriteChar
mov al, [charArr+1]
call WriteChar

exit
main ENDP


swap2 PROC
push ebp ; save EBP
mov ebp, esp

mov eax, [ebp+8] ; offset of first parameter
; EBP+8 is an offset. when dereferenced, it points
; to the offset of the first array element (yeah, offset of an offset)
; EAX has now the offset of the first array element
;mov al, [eax] ; this would give the value of the first array element


mov bl, [eax] ; Value of first parameter
mov eax, [ebp+12] ; offset of second parameter
xchg [eax],bl ; bl=val of second param. Second param is set to first param.
mov eax, [ebp+8] ; offset of first param
xchg [eax],bl ; first param is set to second param




pop ebp ; restore EBP
ret 8
swap2 ENDP
END main

Darkwraith
Jan 2nd, 2005, 11:34 AM
; push the characters onto the stack
push OFFSET [charArr + 1]
push OFFSET charArr
call swap


Methinks it was the fact that you should have either not have had brackets or had brackets (this fact changes from assembler to assembler.)