Author Topic: learning ASM  (Read 10754 times)

Azzazelus

  • Guest
learning ASM
« 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:
Code: [Select]
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.

jj2007

  • Member
  • *****
  • Posts: 9939
  • Assembler is fun ;-)
    • MasmBasic
Re: learning ASM
« Reply #1 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
.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:

Vozzie

  • Guest
Re: learning ASM
« Reply #2 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.

Code: [Select]
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
« Last Edit: October 04, 2012, 01:27:20 AM by Vozzie »

mineiro

  • Guest
Re: learning ASM
« Reply #3 on: October 03, 2012, 09:06:18 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


Azzazelus

  • Guest
Re: learning ASM
« Reply #4 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.

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1209
Re: learning ASM
« Reply #5 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.
Code: [Select]
;==============================================================================
; 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
Well Microsoft, here’s another nice mess you’ve gotten us into.

ToutEnMasm

  • Member
  • *****
  • Posts: 1189
    • EditMasm
Re: learning ASM
« Reply #6 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.
Fa is a musical note to play with CL

Azzazelus

  • Guest
Re: learning ASM
« Reply #7 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.

jj2007

  • Member
  • *****
  • Posts: 9939
  • Assembler is fun ;-)
    • MasmBasic
Re: learning ASM
« Reply #8 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 in the old forum.

Azzazelus

  • Guest
Re: learning ASM
« Reply #9 on: October 03, 2012, 10:55:54 PM »
So its ok to continue with the book I started ?

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6881
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: learning ASM
« Reply #10 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

mineiro

  • Guest
Re: learning ASM
« Reply #11 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

Vortex

  • Member
  • *****
  • Posts: 2066
Re: learning ASM
« Reply #12 on: October 04, 2012, 03:06:20 AM »
Hi Azzazelus,

Another source for learning :

http://www.plantation-productions.com/Webster/

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: learning ASM
« Reply #13 on: October 04, 2012, 09:06:37 AM »
Hi Azzazelus,

please check also the following source: http://www.movsd.com/links.htm. There's a lot of useful information.

Gunther
Get your facts first, and then you can distort them.

japheth

  • Guest
Re: learning ASM
« Reply #14 on: October 04, 2012, 04:02:36 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. 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.