News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

A question about pop and push

Started by Alawna, June 07, 2006, 05:21:36 AM

Previous topic - Next topic

Alawna

Hi everybody. I am new to all this. I did use win32 API externsively in the past but no experience in assembly except for reading scatterd chapters from some books.
I was looking at the 3rd iczelion tutorial where he creates a basic window. Wanted to know why he used push and pop here insted of "mov wc.hInstance, hInstance"?
    push  hInstance
    pop   wc.hInstance

thanks

hutch--

Alawna,

x86 hardware does not support memory to memory copy so it has to be done 1 of 2 ways, either by using the stack as wih PUSH / POP or by using a register.

In non critical code which this instance is, the stack is a better way to do it but in the middle of an intensive algo you would use a register as it is faster.


push var
pop othervar  ; stack method

mov eax, var
mov othervar, eax  ; register method.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Alawna


ollydbg

#3
move macro a,b
       push b
       pop a
endm

You can do it like this:
move mem1,mem2

Ossa

Quote from: ollydbg on June 07, 2006, 10:54:59 AM
move macro a,b
       psuh b
       pop b
endm

You can do like this:
move mem1,mem2

Can I make a few suggestions here?

1) "psuh b" should be "push b" - just a typo, but important none-the-less (and "pop b" should be "pop a").

2) You might want to change your macro a bit:

move macro a:REQ, b:REQ
    push b
    pop a
endm


this will make some errors a bit more understandable, as it means that both parameters must be present.

3) A slightly more advanced macro would be this:

m2m MACRO DEST, SRC
IFB <DEST>
.ERR <m2m requires two parameters>
EXITM
ENDIF

IFB <SRC>
.ERR <m2m requires two parameters>
EXITM
ENDIF

IF ((OPATTR (DEST)) AND 00000010y) AND ((OPATTR (SRC)) AND 00000010y)
; If both are memory locations

push SRC
pop DEST
ELSE
; otherwise

ECHO Warning: m2m called without both parameters as memory locations - switching to mov

mov DEST, SRC
ENDIF
ENDM


this will produce very readable errors when use incorrectly and will also switch back to mov if you used appropriate parameters (it also produces a warning to this effect).

Hope someone finds these useful,
Ossa
Website (very old): ossa.the-wot.co.uk

drizz

Can I make a suggestion here?  :bg
Quote from: Ossam2m MACRO DEST, SRC
:::
m2m MACRO DEST:REQ, SRC:REQ
   push SRC
   pop DEST
endm

req is for obligatory arguments, also this m2m is better because it can take
both regs, imm and mem as arguments
so you can use "m2m ecx,5" which is only 3 bytes, for example
The truth cannot be learned ... it can only be recognized.

Ossa

drizz, I'm not too sure what you're saying here:

1) I already showed the REQ in the first macro in my post above, but I prefer the error messages produced by the second macro.

2) Are you referring to the macro that you posted or that I posted? I'm not quite sure.

Quote from: drizz on June 07, 2006, 11:51:01 AM
req is for obligatory arguments, also this m2m is better because it can take
both regs, imm and mem as arguments

Is this a clarification of what I was saying or are you saying that the simple one that you showed in your post is better?

Quoteso you can use "m2m ecx,5" which is only 3 bytes, for example

[edit] Ok, I get it now [/edit]
[edit] The reason that I would use my version is for speed, a single operation is faster than 2 [/edit]

Sorry, I'm just a bit confused,
Ossa
Website (very old): ossa.the-wot.co.uk

The Svin

Quote from: hutch-- on June 07, 2006, 05:26:14 AM
Alawna,

x86 hardware does not support memory to memory copy

To be precise, it actually does, but not with direct addressing.
PUSH \ POP are also mem to mem copy.
as movsd\w\b
it because of format of x86 machine code.
modrm (block of opcode where address of operand specified) has the only one mem. operand place for specifying address with full abilities of modrm encoding.
However if one or both mem. address assumed by opcode group (in case movs [edi] [esi], in case pop - [esp]) opcode can copy mem to mem.

Ratch

Alawna,
     I push every member of the WNDCLASS structure onto the stack, and then INVOKE RegisterClass with the stack address pointing to the WNDCLASS structure.  PUSHes use less memory than MOV's, and I suspect they might be faster too.  I can send you a code snippet if you are really interested.  It seems to me to be more efficient, and that is the name of the game with MASM isn't it.  Ratch

drizz

Quote from: Ossa
1) I already showed the REQ in the first macro in my post above, but I prefer the error messages
heh, sorry, overlooked it, gotta mop my eyeglasses better... :)
The truth cannot be learned ... it can only be recognized.

TheGreatJason

Hi, I am another person just starting to get familiar with the workings of assembly code, and maybe I will soon be able to write some things too.  The only language I have much experience with is BASIC (VB6 I am most used to).  As I hear a lot, this doesn't help you a lot to learn the basic functions of the computer and proper programming techniques, (and it isn't very efficient or fast either) so I'd thought I'd come down to the lowest level I can before I have to actually be a computer to just read it! :lol

I learnt a lot of VB just by looking at examples and going through tutorials, basically just familiarising myself, and I can write some half decent programs with it now after some years.  Will I be able to understand assembly code in the same way?  These iczelion tutorials suggest so.  But I come to just number 3 and already it stops making sense.  I think that's just because the tutorial assumes too much knowledge on the ones reading it.  Everyone I have talked to either hates the concept of programming computers (i.e. doesn't have the mental capacity or patience to do it) or says that assembly code is too complicated and that is why everyone uses high-level languages to write programs (or black box technology as I would call it).  This is instantly disproved by the very existence of this forum.  Most people are happy to live in the "ignorance is bliss" world, but I am a mathematician, and I am probably more in favour of the Socrates saying: "There is only one good, knowledge, and one evil, ignorance."

Of course, people always make the inside of a black box the most simple thing possible, so I expect that I have made some horrible assumptions about how to program by now.  I am totally prepared to unlearn anything that I need to to learn assembly, and hope that it isn't too much... :eek

Obviously, from a practical point of view, assembly code is no good compared to high-level languages because it just takes too long to do and can so easily go wrong.  (Or is that a myth too?)  But I am not learning this because I want to write programs that are able to perform useful tasks, but because I want to know what the limits of computers really are, to write programs that fully utilise all the computer hardware and make them perform at the best possible efficiency and speed.  In my opinion, the rapid development of computer hardware will eventually lead to the manufacture of unnecessarily inefficient programs that just don't work on older computers because of layers of rubbish development tools that make the machine code into a huge mess.  Assembly has got to be the only way to do things right.



Now back to my real questions (based on the 3rd tutorial)

Firstly about the difference between PUSH/POP and MOV.  What exactly do they do, and how do they differ in speed/memory?  There seems to be some contradictory comments here, and what am I supposed to believe?

Secondly, there seems to be several ways that data is passed around in MASM32: some variables and registers seem to be just named directly, then there is OFFSET... used to pass both some initialised variable and a procedure in this tutorial!  There's also ADDR.  Which contexts are each of them meant to be used in, what's the difference between them and is the data copied or just referenced to in each case?

Next, what is the memory structure and how is it used in assembly code exactly?  What are the main registers called, how do you access them all, how is the main memory used, and the caches?  Where is everything stored and where does the stack come in?  How does the stored program idea work in assembly code?  (Where is the program actually, and how is it referenced to etc.?)

Final questions...  What are the .lib files and linking objects thing about?  What exactly is NULL for?  Are all the higher level commands in MASM32 just contractions of longer assembly sequences (and where can I find out about them)?


OK, I don't expect you to answer them all by yourself with a ridiculously long post like this one.  (Don't let that stop you though!)  But I do want all these questions answered!  I'll be perfectly happy with a link to a webpage that explains any of this stuff, or even better, a tutorial or a few tutorials about these things.  As I explained before, finding help about writing is very difficult compared to all the high-level languages, so finding a well made website that doesn't assume you already understand assembly is really not easy.


Looking forward to any replies to this.  And to being able to write my first assembly program from scratch!  (Well as close to scratch as you can get with this...)  I hope I've made how much I know about assembly so far has been made clear...  :red  Thanks for reading!

hutch--

Hi Jason,

Welcome on board. With MASM you mainly pass data by its address, literally where it is in memory. OFFSET gets you an address that is known at assembly time and is usually in the data section. The ADDR operator is an internal one for the "invoke" syntax and it will work on both an OFFSET in the data section and a LOCAL value that has its effective address loaded into a register. Its done like this.


YourProc proc args etc ...

    LOCAL MyBytes[128]:BYTE
    LOCAL pBytes :DWORD

    lea eax, MyBytes
    mov pBytes, eax


The ADDRESS of MyBytes is now in a DWORD veriable that functions as a POINTER to that address.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

GregL

Jason,

Hi, and welcome. Once you get the 'hang' of assembly language you will love it. It is addicting.

Quote from: TheGreatJasonObviously, from a practical point of view, assembly code is no good compared to high-level languages because it just takes too long to do and can so easily go wrong.  (Or is that a myth too?)

I would say that's a myth. Once you learn assembly language, it is no harder to write a program in it than in C. Some of the high level langauges like VB do make some things easier, but they hide the low level details from you, so you don't really understand what's going on "under the hood". Some things are easier in a high level language, but conversely some things are easier in assembly language. And you can't beat the speed and the size of the executables.

The Iczelion tutorials are great, but they assume some knowledge of assembly language basics. The MASM Programmer's Guide is a great reference. The Intel Manuals are very good too. Art of Assembly Language is a good book, I just wish it targeted MASM instead of HLA (High Level Assembly), you can still learn a lot from it. There is a book you can buy, that I think is very good for learning MASM, but it's expensive (it's a college course book). There are three chapters available here. Hang in there and learn Assembly Language, you won't be sorry. You will become a much better programmer for it.




ChrisLeslie

TheGreatJason

You probably should have started your own thread! But never mind.
QuoteThese iczelion tutorials suggest so.  But I come to just number 3 and already it stops making sense.
The tutorial can be a big hurdle in the early stages, but if you can get past about number 4 or 5 then things will actually be easier. If you don't have it already http://win32assembly.online.fr/tutorials.html provides good theoretical explanation for the examples.

I have only started using Win32 assembly this year on a casual basis but I can honestly say that I would prefer to use it to start any new job of small to medium size at least. I tend to think of, and use, Win32 assemblers as a high-level language as I make full use of HLL structural macros and because a great deal of code is really invokations of APIs. Used as such assembler can be comparable in terms of ease of use as many high-level languages but also puts you in much greater control. Not everybody agrees with that approach and would prefer to write in a more traditional low-level style, but such variation in approach demostrates how flexible assembly can be.

Keep at it. The concepts will start to make more sense when it occurs to you that things are done the way they are basically because we are talking more directly to hardware rather than obeying abstract concepts.

And one last thing, you will find that this forum is about the best around in terms of friendly and mature responses.

Regards  :U

Chris

TheGreatJason

#14
Wow, 3 quality replies in just 13 hours to a complete stranger.  You're right, this forum must be one of the best around! :cheekygreen: Or do I mean the best around.  Well, I'm not interested in going anywhere else after I already downloaded all the stuff, and this is what I was looking for, so look no further...

Thanks for all the links, that is what I was looking for, and I probably won't ask any more basic questions unless I get stuck somewhere on all of the help documents at the same time.  (Now that would be annoying)  The pdf tutorials/manuals should be very useful.  Although, is there anywhere I can get the MASM Programming Guide as a single PDF for off-line reading?  OK, I'll just download them all... ::)  And thanks for that book recommendation.  If I see it going cheap in a carboot sale or a charity shop then I'll be sure to buy it.  To increase the chance of that happening, what other books can you recommend of the same quality?

I hope learning assembly will be an interesting experience for me!  And thanks for you help!

Cheers,

Jason

Edit:
Quote from: MASM Programmer's GuideThis Programmer's Guide is written for experienced programmers who know
assembly language and are familiar with an assembler.
Hmmm...