News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

new member introduction and advice

Started by q12, September 04, 2021, 12:42:56 PM

Previous topic - Next topic

hutch--

Hi q,

Here is a small test piece for you. Save it as an ASM file in a directory by itself then build it as a console app.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey                           ; wait for a key press to exit
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    push esi                        ; save esi

    mov esi, 500
    add esi, 500                    ; ADD
    print "Result 1 = "
    print str$(esi),13,10

  ; ---------------------------------

    mov esi, 2000
    sub esi, 1000                   ; SUB
    print "Result 2 = "
    print str$(esi),13,10

  ; ---------------------------------

    xor esi, esi                    ; clear esi (set it to zero)

    mov eax, 100
    mov ecx, 200                    ; MOV = copy 200 into ECX register
    mov edx, 400

    add esi, eax
    add esi, ecx
    add esi, edx
    print "Result 3 = "
    print str$(esi),13,10

  ; ---------------------------------

    pop esi                         ; restore esi

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start

q12

Thank you mister @hutch
Your test code, built and run successfully. Your code is both a tutorial and a test. Which is good. It is helpful to me.
I love good commented code lines !!!
My great achievement for today is that I managed, thanks to your kind help, to successfully start and run the assembler on my PC, by using the console and the textEditor. It is a great step for me.
Excelent ! And I am almost done with the console tutorials, Im on demo6. I did concentrate on them. They are very condensate and very well done !!! I really like how they were built to help the beginner. They managed to help me quite good. Because of them I started to actually write, a little bit, some very simple instructions. I also come across some errors that I comment them out and I will show the entire code I built out from those demos.
Here is your new test code built and run successfully:
-Thank you for your help!


q12

Alright ! I managed to finish all the console demo files!I also finish your test, mister hutch.
By finish, I mean I rewrite/copied the code from right to left, manually, by hand, and made a single file that contains all these codes from all the files.
It is a compilation/gathering of all codes in one single file.
Nothing fancy, but it give me a challenge and unbelievable, I managed to make it work from the start.
I had a lot of problems with the structure of the code, where to put what, but I learned a lot this way. I am way more better than 2 days ago. It is a great advance for me. THANK YOU! I could'n done it without your help here, im refering to all those who helped me until now.
When code is assembled, I have some warnings from the last include but I imagine,  it contains the same include's as the ones before it (i didnt open it)but it is working fine after those duplicate warnings.
I commented some parts of the code that didnt worked for me in this full code page. But those codes worked by their own in their original demo files. My guess, there are some location or precedence most probably that does not allow the proper function of those specific lines to work.
Check this reply attachment for my code file:

hutch--

Something that you must learn is what registers you can routinely use and which registers you have to protect. Its called the "Intel Application Binary Interface" (ABI). You can routinely use EAX, ECX and EDX but the rest must have the same value on exit as they had on entry. Normally done with PUSH to save and POP to restore, it allows you to use EBX ESI and EDI. The other two, ESP and EBP should be left alone until you know a lot more about what they are used for.

Now in a simple example, if you need more than the three that can be routinely changed, you use code like the following.

push esi
push edi
; use the regs
pop edi
pop esi

Note that the pushed and pops are done in reverse order as the are pushed onto the stack memory and popped back off in reverse order.

q12

The operands are simple to deal with, for example: mov,add,sub,xor
But the instructions are a big unknown to me. I need a short and to the point introduction to what these instructions esi,eax,ecx,edx means and  as simple to understand them. I need a short  list of them (the easy or most common)with proper description + a general description of the entire set/register that contains them. If you please.
I'll think on an exercise to use the push and pop commands, to get more used to them.
I see in your code:  pop esi   at the end.
Thank you for the explanations on the push and pop instructions, I understand them now, but they are too new to me at this point. I need experience using them, to actually remember them and to mean something. I have a general idea about them, but at a rookie level. They are not completely new to me. So... more exercise with them and more experience. Definetly I will keep an eye on them as you told me here. Thank you.

Vortex

Hi q12,

You can also try coding a message box application, quick and easy.

q12

To mister Vortex
"..try coding a message box application.."
With only the information I got from the console demo ?
I think I need a better tutorial for that. I will listen to your advice and start looking for such a tutorial.
I looked inside the tutorial folder from masm32 and I find another 3 folders, that deal with 1-how to make a dll, 2-logaritmic math stuff, 3-portable executables. They are more advanced than simple introduction to newbies like me. I could start one of those if you specifically recommend them. Your recommendation is gold to me at this point.
I know I tried in the past with assembler and windows controls and it was hell, I dont think I managed to make anything, not even a simple empty form, back then. But I dont remember the context, I think I was still in c# and tried something out of the box, I think; I really dont remember the details, only the great failure that cost me like ... a couple of weeks of reading and trying and failing and give up in the end, and follow my usual and normal routine with c#.  I think it was c#...hmmm. Totally forgotten that episode, only retain the title from it "Failure". Haha.  But now I have you guys so it's completely other context than doing everything alone like before.
I did listen to the instructions on the how to begin page,here on this forum, and from many things recommended there, I installed this ollydbg.exe program for debugging. I managed to start it and load the executable from demo console folders. It works. It is loading them and showing me another world here that I have absolutly no idea what I am looking at. I imagine it is a part of ram that program is accessing, is my best guess.Hmm. I did come across debuggers/disassembles  like this in my life, one was for cheat in games and I am aware such things exist, but I never get too familiar with them, only on the surface.
I think I need another tutorial at this point. A easy and more in depth tutorial. About any subject, I dont care, as long as it is bringing light over some notions I've seen a lot but never truly knew what are and where they come from. Hmmm... We'll see.

hutch--

Hi q,

The MASM32 SDK is really pointed at more experienced programmers who have worked in other languages so with C# you are at the entry level. We will help you out where we can but its a steep hill you are climbing and there are many steps to climb. Have a look at the help files in the editor as this will give you a lot more background.

For instructions (mnemonics) the best available is the Intel manuals which you get from their site. One of the members may have a simpler introduction to the instructions but I don't have any available myself.

Now as far as writing tutorials, if you are willing to write the 64 bit code I am working on to my satisfaction, I may write some 32 bit tutorials but really I don't have the time to do that.

q12

I understand mister hutch, don't worry. I know it is a very hard  and complicated subject.
(If you are writing something for me, I will be more than happy to try it, to copy it manually like I did until now)
But in general, you will not have to write any tutorials for anyone.
Just point me where to look, what to read, what tutorials, what steps to advance.
Like you did point me perfectly to start with the console demo codes, that really introduced me good enough.
O K , Intel manuals then... I never ever in my life touch Intel website except when I followed a recommendation these days and downloaded already the "Intel Pentium 4 Design Manual.pdf" and inside ... oh boy, haha.
I'll have to search for instructions this time - I understand and I will do exactly that.

Also, an answer for mister Vortex
I did google for a messagebox code and I found one, fix on this website:
http://masm32.com/board/index.php?topic=4015.0
it's the first code that user posted in his question.
I read it, and understood some part of it now, after the console introduction I have, I CAN SEE THINGS !
I will point out what I don't understand from that code:
push 0   ;WHEN to use these push and pop instructions and for WHAT purpose? -very noob with them
(like mister hutch suggested earlier, to look into push and pop instructions, this code serves a good example and I put it aside in my tutorials folder)
next:
mov eax,  offset MyTitle
push eax
mov eax, offset MyText
push  eax

That eax thing is bugging me greatly.  WHEN to use these instructions and for WHAT purpose? -very noob with them
You don't have to explain here anything, don't worry. Just point me where to look for the answers.
I think mister hutch give me already the path to look into Intel instructions manual, so Ill do exactly that, because it sounds logical.
If anything important to mention, please do so.

- Thank You !






hutch--

Here is another small test piece. If you use the manual include files, you don't use the masm32rt.inc file as it duplicates the manually included files. The masm32rt.inc is easier to use. This test piece shows somewthing basic about x86 instructions, you cannot use direct memory to memory, it must be done through a register. The reason why the ESI register is used is that the common registers, EAX ECX and EDX can be overwritten by other code where the registers that must be protected cannot be.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey               ; wait so you can see the results
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL var1  :DWORD
    LOCAL var2  :DWORD

    push esi

    mov var1, 1234              ; load memory operand with immediate number
    mov var2, 5678

  ; add var1, var2              ; this generates an error
                                ; there is no OPCODE for memory to memory copy

  ; --------------------------------------------------
  ; adding memory operands must be done via a register
  ; --------------------------------------------------
    mov esi, var1               ; copy operand to register
    add esi, var2               ; add 2nd operand to register
    mov var1, esi               ; copy register back to operand

    print "total = "
    print str$(var1),13,10

    pop esi

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start

q12

Thank you for the tutorial. Very nice explained. I put it aside in my tutorial folder.
Can you find for me those Intel mnemonic books?


q12

"..Registers are a special working area within the processor..there are eight general-purpose registers, EAX, EBX, ECX, EDX, ESI, EDI, ESP, and EBP.  In most instances, ESP and EBP should remain unused as PowerBASIC uses them for entry and exit of procedures. This means that you have six 32-bit registers to use in your assembly code.."
I find this little nice explanation of what those registers are. Ok, so they are registers inside my CPU and not my RAM as i originally guessed.
Why are so many and when to use one or another? That's my next question.

update1: I've made an account into www..intel.com
update2: I find this page:
https://software.intel.com/content/www/us/en/develop/download/intel-64-and-ia-32-architectures-sdm-combined-volumes-1-2a-2b-2c-2d-3a-3b-3c-3d-and-4.html
and downloaded this cute and short introduction pdf that has 4778 pages and it will take me 10 years + to finish reading it fully.
And is also having 4 Volumes in one pdf. I seriously downloaded it and now im looking in it... but I see nothing that can help me yet ... hmm, maybe I will get lucky.. in 10 years or so. Haha, I'm laughing with tears. Im starting to search things in it though... I hope is the right book (with 4 Volumes in it) lol.  - Please confirm if it's the right thing to OPEN and read or not. Since I did it by instinct until now.

hutch--

Intel provide a number of formats, 4 separate manuals or all 4 combined. Either will do and look for the"Instructions" manual. There are a hell of a lot of them, high hundreds but the ones you want now are the simple integer instructions, MOV ADD SUB SHL(R) ROL ROR CMP XOR OR etc .....

I don't have one for you but there have been simplified collections that are much easier to follow. The Intel manuals are by far the best but you get swamped with information that is not always easy to understand.

Once you get the swing of the ordinary integer instructions, we will show you how to read and write "complex addressing mode" that looks like this.

mov eax, [ecx+edx+64]

Among other things, its used for arrays and is a critically important technique for addressing memory.

Here is one link that may be a bit easier to work with.

https://www.felixcloutier.com/x86/add

q12

Thank you mister hutch !
So... im not crazy, it is a LOT of information there. Hmmm. I thought I am the UN-lucky one. Since you didnt say anything (bad) about my Intel link, I consider that I find the right book !!!
As I said, I started to actually read through the book (of death), I actually jumped directly to what is most critical for my knowledge : GPR (general purpose registers) and I understand their clasifications now, 8,16,32 and (the newest) 64 bytes.  They kind of put little bits of information in 1 chapter and then to another chapter a couple chapters down. SO I had to (mindlessly) scroll down, and lucky me, I found in a couple of chapters, mentions of this GPR that I am most interested into. Still not there yet, since they explain the locations and dimensions, A LOT, but not really what is important (to me), which is... what are they good for? I'm not sure if I ask the right question now after reading all of that... As a general idea, the GPR are used for aritmetic and logical operations... but... in practice !!! I can not see the practical side of them, yet. I'll hammer this book these days as much as I can and as far I can go. If Im getting this basic thing, oh boy... GPR is basic right? is it an advanced thing? I dont know. You tell me.
I also started to collect these chapters into a single text file. And after that, Ill have to condense again, what I just collected. Hopefully, that's the plan. It's a crazy lecture, I tell you that. What is interesting though, over all that terminology mambo-jambo, I do understand the big idea, and in a sense, it is kind of easy to read... or maybe I am too purpose driven right now, thats why I can fly over things more easily. That must be it... Anyway... Ill update you about my evolution (hopefully).

Update1:
I think I just find your thing you wanted to teach me in your example:
"mov eax, [ecx+edx+64]"
3.7.5 Specifying an Offset  (at page 86)
Figure 3-11. Offset (or Effective Address) Computation
Base    Index     Scale     Displacement
[eax] + [eax    *   1]   +  [8bit]

q12

I need clarification:
Intel say: "

MOV     Move data between general-purpose registers; move data between memory and generalpurpose or segment registers; move immediates to general-purpose registers.

- What  "memory" is he talking about? The RAM memory? Or some other type of memory? Is CPU having a memory? I am inclined to think he is refering to CPU memory. 


5.1.8 String Instructions
The string instructions operate on strings of bytes, allowing them to be moved to and from memory

- Again, What  "memory" is he talking about?

From my general knowledge:
A CPU is having only registers addresses.
A RAM is having only memory addresses.
Is this information I know complete ? Or there is something more?