The MASM Forum

General => The Campus => Topic started by: Azzazelus on October 03, 2012, 07:59:50 PM

Title: learning ASM
Post by: Azzazelus on October 03, 2012, 07:59:50 PM
Hello there! I'm a begginer and started recently to learn ASM from "Assembly Language For x86 Processors Sixth Edition, Kip R. Irvine".
I want to understand absolutely everything correctly so Ill post here some beginer queations as a go thro the book and dont understand some things.

1. I understand how to declare variables, how to move them to an location (such as EAX), add, substract and work with different radix. What I dont understand is what actually multiple initialisation of a variable do.
For example:
list1 BYTE 10,20,30,40;
In the book says it occupies a BYTE with data stored like  10|20|30|40 in the same byte. But Visual Studio debbuger only shows me the first initializer (10) as beeing the default value. So its a vector of more BYTES or just data in a single BYTE. What about if I exceed the BYTE limit and declare more variables?
Thanks.
Title: Re: learning ASM
Post by: jj2007 on October 03, 2012, 08:40:37 PM
I doubt that the book says "stored in the same byte". Kip's book may be a little bit outdated, but he is usually correct ;-)

What happens here is that you declare a variable in the .data section with size BYTE (i.e. 0...255). The variable has four elements, i.e. it is a BYTE array:
.data
list1 BYTE 10,20,30,40   ; no need for a semicolon, unless you want a comment


Here is an example how to access the elements of the array:
include \masm32\MasmBasic\MasmBasic.inc   ; download (http://masm32.com/board/index.php?topic=94.0)
.data
list1 BYTE 10,20,30,40

   Init

   mov esi, offset list1      ; point a register to the array
   movzx eax, byte ptr [esi+0]
   movzx ebx, byte ptr [esi+1]
   movzx ecx, byte ptr [esi+2]
   movzx edx, byte ptr [esi+3]
   deb 1, "The array:", eax, ebx, ecx, edx      ; use a debug macro to show results

   Exit
end start

The proggie will show a MsgBox:

The array:
eax             10
ebx             20
ecx             30
edx             40


By the way: Welcome to the forum :icon14:
Title: Re: learning ASM
Post by: Vozzie on October 03, 2012, 08:42:12 PM
Hi,

Learning assembly myself i'm not the best to answer, but...

list1 is a BYTE variable containing 10. But the value of ((the address of list1) + 1) contains 20. So you have 4 values in a row/array. 10,20,30,40. If you take the address of list1 you have a pointer to a array.


include \masm32\include\masm32rt.inc

.Data

list1 byte 10, 20, 30, 40

.Code

start:
Lea Eax, list1
Movzx Ecx, Byte Ptr [Eax + 0]
Movzx Ecx, Byte Ptr [Eax + 1]
Movzx Ecx, Byte Ptr [Eax + 2]
Movzx Ecx, Byte Ptr [Eax + 3]
Invoke ExitProcess, 0

End start


For learning assembly i would not use visual studio. It's like killing a fly with a nuke...

Did you download the MASM32v11 sdk?

I wrote my first programs in MASM32's QEditor. You can download a debugger like OllyDbg and change qeditor's menu.ini to launch the debugger from qeditor...

Then there are many IDE's who are more lightweight and i think better for learning assembly,... I prefer EasyCode because of it's easy setup, a good resource editor, code completion/highlight etc... I use EasyCode in conjunction with ollydbg. It runs much faster then visual studio and i have everything there to learn/write assembly...

(Visual studio doesn't have code completion and syntax highlighting etc for assembly)

Welcome to the forum
Title: Re: learning ASM
Post by: mineiro on October 03, 2012, 09:06:18 PM
Quote from: Azzazelus on October 03, 2012, 07:59:50 PM
In the book says it occupies a BYTE with data stored like  10|20|30|40 in the same byte.
two bodies cannot occupy the same space at the same time;
Quote
What about if I exceed the BYTE limit and declare more variables?
body1 byte 10,20
body2 byte 30,40

Title: Re: learning ASM
Post by: Azzazelus on October 03, 2012, 09:19:09 PM
Thanks to all for such detailed and fast answers.
Yes, actually the book doesnt say " in the same BYTE" and its actually what I badly understood.
From your cod now I understand perfectly.
Im using Visual Studio because I also code in C++ but I will try your tool listed and post an feedback.
Thanks again.
Title: Re: learning ASM
Post by: MichaelW on October 03, 2012, 09:52:38 PM
List1 is a label that represents the starting address of four initialized bytes in the data segment.

;==============================================================================
; Build as a console app.
;==============================================================================
include \masm32\include\masm32rt.inc
;==============================================================================
.data
    List8 QWORD 1,2,3,4
    List4 DWORD 1,2,3,4
    List2 WORD  1,2,3,4
    List1 BYTE  1,2,3,4
.code
;==============================================================================
start:
;==============================================================================

    ;---------------------------------------------------------------------
    ; In 32-bit code the stack must be maintained at a 4-byte alignment.
    ; So to pass byte or word data on the stack you should sign (for
    ; signed data) or zero (for unsigned data) extend the byte or word
    ; into a 32-bit register and pass the register. QWORD (8-byte) data
    ; must be passed in two DWORD operations, but MASM will take care
    ; of this detail for you.
    ;---------------------------------------------------------------------

    ;--------------------------------------------------------------------
    ; This block is a minimal attempt at showing what the printf macro,
    ; used used below, is actually doing. The cfm$ macro expands any
    ; escape sequences in the string that it is passed (so for example
    ; "\n" is expanded to the ASCII code 10), copies the expanded string
    ; to the initialized data section, and returns the address of the
    ; string.
    ;--------------------------------------------------------------------

    mov ebx, OFFSET List1
    movsx eax, BYTE PTR [ebx]
    push eax
    push cfm$("%d\n\n")
    call crt_printf
    add esp, 8

    mov ebx, OFFSET List1
    movsx eax, BYTE PTR [ebx]
    printf("%d\t", eax)
    movsx eax, BYTE PTR [ebx+1]
    printf("%d\t", eax)
    movsx eax, BYTE PTR [ebx+2]
    printf("%d\t", eax)
    movsx eax, BYTE PTR [ebx+3]
    printf("%d\n", eax)

    mov ebx, OFFSET List2
    movsx eax, WORD PTR [ebx]
    printf("%d\t", eax)
    movsx eax, WORD PTR [ebx+2]
    printf("%d\t", eax)
    movsx eax, WORD PTR [ebx+4]
    printf("%d\t", eax)
    movsx eax, WORD PTR [ebx+6]
    printf("%d\n", eax)

    mov ebx, OFFSET List4
    printf("%d\t", DWORD PTR [ebx])
    printf("%d\t", DWORD PTR [ebx+4])
    printf("%d\t", DWORD PTR [ebx+8])
    printf("%d\n", DWORD PTR [ebx+12])

    mov ebx, OFFSET List8
    printf("%I64d\t", QWORD PTR [ebx])
    printf("%I64d\t", QWORD PTR [ebx+8])
    printf("%I64d\t", QWORD PTR [ebx+16])
    printf("%I64d\n", QWORD PTR [ebx+24])

    inkey
    exit
;==============================================================================
end start

Title: Re: learning ASM
Post by: TouEnMasm on October 03, 2012, 10:08:38 PM
Quote
Kip's book may be a little bit outdated, but he is usually correct ;-)
Not just a little,made a search in the old forum and you will see.
Best way to start is to study the samples coming with the masm32 package.
Title: Re: learning ASM
Post by: Azzazelus on October 03, 2012, 10:17:23 PM
I wanted to learn everything step by step thats why I choose that book. If you can point me to other resources better than Irvin book (sites, books) that will contain explanation of the language step by step not just samples I would very much apreciate it.
Also I guess if I want in the future to code on x64 and on new processor architecture the basis of the book will still help me right ? I ask because you said that the book is outdated and I dont want to learn again from other source the same things.
Title: Re: learning ASM
Post by: jj2007 on October 03, 2012, 10:26:37 PM
See my signature (http://www.webalice.it/jj2006/Masm32_Tips_Tricks_and_Traps.htm) for some concise hints.

There are almost no books available; the best proxy is maybe the Masm Programmer's Guide, see link inside this post (http://www.movsd.com/board/index.php?topic=5433.msg40530#msg40530) in the old forum.
Title: Re: learning ASM
Post by: Azzazelus on October 03, 2012, 10:55:54 PM
So its ok to continue with the book I started ?
Title: Re: learning ASM
Post by: hutch-- on October 03, 2012, 11:25:42 PM
You would be better to start with MASM32 if you want a wide range of people who know how to write current and correct assembler code. There is no current book to learn assembler with and the ones available are technically incorrect and out of date. To successfully write 32 bit assembler for the windows platform you must properly handle register preservation (Intel ABI). 64 bit assembler is not fully developed yet and while it is very powerful, it is also a lot more complicated in its stack usage. You will do better to start in 32 bit which is both very well understood and far better supported.
Title: Re: learning ASM
Post by: mineiro on October 03, 2012, 11:29:44 PM
Of course, Irvine book is a didatic one, and the price paid is just that you can create ambiguous situations. It is a bit outdated like other said, but in some sense, it is actual because we live in a imediatist world, so if you do not get results, you will get boring.
The book AoA (art of assembly) is a nice one, but if you like to put your hands on, the better choice in my mind are examples inside masm32 distro.
welcome
Title: Re: learning ASM
Post by: Vortex on October 04, 2012, 03:06:20 AM
Hi Azzazelus,

Another source for learning :

http://www.plantation-productions.com/Webster/
Title: Re: learning ASM
Post by: Gunther on October 04, 2012, 09:06:37 AM
Hi Azzazelus,

please check also the following source: https://www.masm32.com/links.htm (https://www.masm32.com/links.htm). There's a lot of useful information.

Gunther
Title: Re: learning ASM
Post by: japheth on October 04, 2012, 04:02:36 PM
Quote from: Azzazelus on October 03, 2012, 10:55:54 PM
So its ok to continue with the book I started ?

Yes, of course! Don't be confused by the "outdated" attribute that some may attach to the book. That's just FUD.

Many things are "outdated" and still valuable. For example, Masm32 - including the examples - is 32-bit only, while 64-bit is "modern" ( since at least 6 years ).

Another very good, and quite naturally "outdated" book is AoA, which is available online. See http://www.phatcode.net/res/223/files/html/toc.html (http://www.phatcode.net/res/223/files/html/toc.html). If you really want to learn assembly and not just "to code Windows applications in assembly", then such a book is way more valuable than studying - usually sparsely commented - sample code.
Title: Re: learning ASM
Post by: Azzazelus on October 05, 2012, 04:45:11 PM
Hello again. Thanks for the answers.
Now I have another question to clarify some things before I go further.



There is a table in the book that says EAX and AX are general porpuse registers. When I use AX to fill some data, the EAX gets modified. The AX is a register that resides in EAX ?
Later Edit: I found on google that AX resides in EAX).
Also how can I see in OllyDbg the variables from memory ? (oneWord, oneByte,oneDword)?

(http://bobcat-games.net/eax.jpg)
Title: Re: learning ASM
Post by: TouEnMasm on October 05, 2012, 05:22:24 PM

EAX is a 32 bits register         
AX is the low 16 bits part of eax
Al is the low 8 bits part of AX

Same rules for edx,ecx,... except for esi,edi who are only pointer,not general register.
Title: Re: learning ASM
Post by: Azzazelus on October 05, 2012, 05:30:49 PM
In Visual Studio I had the posibility to see variables contents at some point in the program while running with breakpoints. Is that possible with OllyDbg ?
Title: Re: learning ASM
Post by: TouEnMasm on October 05, 2012, 05:36:41 PM

I don't know if olly can insert breakpoint,perhaps he do,search in the menu.
Windbg do it.
In asm you can insert a coded breakpoint.
Quote
invoke DebugBreak
Title: Re: learning ASM
Post by: Vozzie on October 05, 2012, 07:22:17 PM
Hy,

Quote from: Azzazelus on October 05, 2012, 04:45:11 PM
Also how can I see in OllyDbg the variables from memory ? (oneWord, oneByte,oneDword)?

The data of your variables (oneByte, oneWord,...) you can find in the data segment. So in Olly you'll have to watch the memory where that variable resides. The names of those variables(labels) are gone but...

In that screenshot you can see the value of those variables in the left bottom window(Hex Dump)... There you can see

00404000 "78 34 12 87 D6 12 00 00 ....."

You have a Watch Window in Olly where you can enter the offset of those variables and watch them

Byte Ptr [00404000]
Word Ptr [00404000 + 1]
DWord Ptr [00404000 + 3]

Anyway the variable names (labels) are gone but you can also add them again in Olly,...

Click in the HEX viewer on the offset and select ADD LABEL. Give it the name of your variable. Now you can use this name also in the Watch Window  :t

After adding your labels this should work...


When you use EasyCode you have the "Add symbolic debug info" option in your project properties. When you set this option the names of your labels are known and you can just add them to the watch window. When you don't have debug info in your executable then you can label them in Olly manually. (Like i first wrote, i didn't know myself that the symbolic debug info add's the names of labels in your PE).

Open the watch window in Olly and add your labels...

Byte Ptr [oneByte]
Word Ptr [oneWord]
DWord Ptr [oneDword]

To add a breakpoint with Olly, hit F2... To step F8, step into F7... Restart CTRL+F2,...

And to see the program running, hit CTRL+F7, ...

When you add a DebugBreak or Int3 to your code and run the program. That program will show like it crashes in some windows versions but you'll have the chance to click "Debug" (after a while that button shows)... Then you can also debug with Visual Studio. (Make sure in visual studio you have "Native" checked where are the JIT options before you try).

You can also use (set) Olly as JIT debugger (carefull, can be some trouble to get your VS JIT settings back to normal)

There's also a debug library in the MASM package with some handy features...


See the native code, source and watch in this screen shot.
(http://www.vozzie.be/temp/olly.png)

Greetings
Title: Re: learning ASM
Post by: mineiro on October 06, 2012, 12:35:09 AM
Click with right mouse button in that line of code and see what options olly shows you. You have interest in the word 'dump'.

You can insert breakpoint direct in your code, but remember remove that breakpoints if you will not debug your program, like a release version of your program.

main proc
mov eax,0ffffffffh
mov ah,0
mov al,0
db 0cch   ;<--breakpoint direct inside your code, valid if you are on windows, ms-dos or linux
;remember to remove this if you do not need debug anymore
mov eax,0
Invoke ExitProcess,0
main endp

Instead you execute the program step by step, now you are able to run your program inside debug, and it will stop in that breakpoint.
Title: Re: learning ASM
Post by: qWord on October 06, 2012, 01:29:35 AM
Quote from: mineiro on October 06, 2012, 12:35:09 AMdb 0cch   ;<--breakpoint direct inside your code, valid if you are on windows, ms-dos or linux
he can also use the mnemonic: INT 3  :biggrin:
Title: Re: learning ASM
Post by: Vozzie on October 06, 2012, 02:46:08 AM
Just want to add, in EasyCode you have the precompiler definitions to include/exclude code in DEBUG/RELEASE.

IFDEF DEBUG
    Int 3
ENDIF ; IFDEF DEBUG


Or make this a macro for use with EasyCode
DEBUGSTOP Macro
IFDEF DEBUG
Int 3
ENDIF
EndM



The "Add symbolic information" option switches between Debug and Release...
Title: Re: learning ASM
Post by: mineiro on October 06, 2012, 04:59:28 AM
yes yes Sir's, valid points. In a near future he can try non intrusive way, like used by obsidian debug.
ni_bkpt macro
db 0ebh,0feh
endm
Title: Re: learning ASM
Post by: Azzazelus on October 08, 2012, 10:51:10 PM
Hi again and thanks you for the explanations. I'm sticking to EasyCode but I migrated to Jwasm just for the fun of making 64bits applications and using new instructions.
I managed to compile some code with 64bits registers and MMX registers but I cant use for example SSE4 new instrucions as the compiler doesnt know the oprands. Is there a special library that will contain informations about the new instrucions because it seems that Jwasm supports them (in the help file)?
Title: Re: learning ASM
Post by: TouEnMasm on October 08, 2012, 11:29:30 PM

http://msdn.microsoft.com/en-us/library/y0dh78ez(v=vs.80).aspx (http://msdn.microsoft.com/en-us/library/y0dh78ez(v=vs.80).aspx)

you have also the "intel manual"  "AMD manual" and ...
Title: Re: learning ASM
Post by: Azzazelus on October 08, 2012, 11:47:37 PM
The problem is that the compiler doesnt see the new instrucions not that I cant find them on the internet.
Title: Re: learning ASM
Post by: qWord on October 09, 2012, 01:48:17 AM
Quote from: Azzazelus on October 08, 2012, 11:47:37 PM
The problem is that the compiler doesnt see the new instrucions not that I cant find them on the internet.
What compiler? Aren't you using an Assembler?  ;)
If you want to use SSE4.1/2/A, you must use MASM version 9+ or jWasm. To activate such instruction sets, add the .XMM directive to you code:
...
.xmm
...