I'm trying to make my code obscure and hard to read :grin:, but
mostly I just want to learn about some basics to assembly so i
can read other peoples code better.

I'm translating one piece of code..

I've been pushing everything and calling functions now, but I've
read about stack balancing and I'm not sure what i have to do
about it.

Second I have a basic logic statement that I don't really know
how to translate properly..

It's:


.if eax != 0
;do stuff
.if eax == 0
; do stuff
.endif
.else
;do stuff
.endif

And i've got it partially worked out to:


;findwindow
cmp eax, 0
je display_error_open
;post message
cmp eax, 0
je display_error_post
ret
display_error_post:
;messagebox
ret
display_error_open:
;messagebox
ret

I won't show the specific code, but i think everything is working,
can't really test the display_error_post: part though....

Lastly, on the subject of addresses and offsets.

Are these true?:

Only use ADDR when using invoke, don't use offset(?).
push offset A_Variable ; is correct if i need address to a variable?
push ; memory address of previously undefined variable

..

That's all.. if someone could help, much appreciated, a conformation
of some things, a correction of others, and a link to that
assembly guides (not the hll one that i always find instead)
would be appreciated.
Posted on 2002-02-21 01:44:55 by matthew


.if eax != 0
;do stuff
.if eax == 0
; do stuff
.endif
.else
;do stuff
.endif




cmp eax, 0
je @F

;do stuff

cmp eax, 0
jne out

;do stuff

@@:
;do stuff

out:


;I think that is should work perfectly

Sliver
Posted on 2002-02-21 01:56:59 by Sliver
The offset instruction tells the assembler to write the address of the variable in memory as the argument. This can obviously be done with variables that are defined in a known position at assemble time! Local variables can never use the offset instruction because their address is unknown until the process is called (as the exist on the stack, and could exist anywhere in the stack, and the stack itself may not be in the same place either)!

MASM will only allow you to use ADDR with an invoke statement, but that does not mean you cannot use an offset instead.
ADDR simply does something like this:


IF variable is LOCAL
lea eax, variable
push eax
ELSE ; Must be defined, and have a known location
push offset variable
ENDIF


Mirno
Posted on 2002-02-21 03:56:47 by Mirno
matthew,

balancing the stack is normally done by the PROC capacity in masm so if you want to code it manually, you need to make sure it is at the same address AFTER the proc as before it.

The stack is memory that is reserved for the 2 mnemonics PUSH/POP and for passing parameters in most instances.

When you CALL a proc, the return address is placed on the stack so any parameters and locals you want must be added in the right place or you will not have the return address in the right place after the proc is finished.

Probably the best way to get a look at what is happening is to write some normal PROCS and then disassemble them to see what they are doing. MASM uses LEAVE at the end to tidy up the stack. Its shorter from memory but its mainly used to prevent a register stall.

Just try a few of these for practice. The simplest CALL is one with no parameters to a label with a RET after it.



CALL MyLabel

; asm code

MyLabel:
; more code
RET

This works fine in simple situations.

push parameter
call NextProc
; more code

NextProc:
mov eax, [esp+4]
; code that does NOT use ESP or EBP or LOCAL vars
ret

A more authodox proc call

push parameter
call thirdproc

; more code

thirdproc:
push ebp ; preserve base pointer
mov ebp, esp ; stack pointer into ebp

mov eax, [ebp+8]

; other code

mov esp, ebp ; restore stack pointer
pop ebp ; restore base pointer

ret


Decompile a MASM exe to see how MASM does it.

Regards,

hutch@movsd.com
Posted on 2002-02-21 08:50:17 by hutch--