flat assembler
Message board for the users of flat assembler.

Index > Windows > Assembly language for beginner

Goto page 1, 2, 3  Next
Author
Thread Post new topic Reply to topic
Stefx



Joined: 24 Apr 2020
Posts: 21
Stefx 25 Apr 2020, 16:31
Hi everyone,
I am completly "noob" in this language but after all I really want start to learn.
I would like ask what, where and how I should learn on beginning?
I am using FASM so I wanna keep this IDE Smile
Thanks for the answers.
Post 25 Apr 2020, 16:31
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 25 Apr 2020, 16:34
Recently I have been making very basic video tutorials intended for beginners like you. Please try them out!
Post 25 Apr 2020, 16:34
View user's profile Send private message Visit poster's website Reply with quote
Stefx



Joined: 24 Apr 2020
Posts: 21
Stefx 25 Apr 2020, 16:45
Thank you for your answer
I watched all your material, but unfortunately I have a problem with understanding such instructions as db, mov, push, call, edx, ecx and so on.
What are these instructions for, what do they do, and what can I do with them?


Last edited by Stefx on 25 Apr 2020, 16:58; edited 1 time in total
Post 25 Apr 2020, 16:45
View user's profile Send private message Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 718
Ali.Z 25 Apr 2020, 16:46
after checking the videos you may want to learn about the calling conventions, then assembly instructions.

also do not forget to check the examples folder for the particular operating system you are on.

there is also a dedicated subforum for examples and tutorials, please check them out
https://board.flatassembler.net/forum.php?f=23

finally, you can always ask here and most likely you will get the answer.

oh almost forgot, welcome to assembly the best and easiest language ever.

_________________
Asm For Wise Humans
Post 25 Apr 2020, 16:46
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 799
Location: Russian Federation, Sochi
ProMiNick 25 Apr 2020, 17:03
Stefx, if thour host OS for development is windows.
Thou could look at my include package https://yadi.sk/d/HTs1wxZvp6BmYw
there are examples with same functional for win16/win32/win64/wince to demonstrate windows is windows anywhere (if thou interested in windows of course).
also it is demonstration how thou could setup fasm package by thour needs.
Post 25 Apr 2020, 17:03
View user's profile Send private message Send e-mail Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 718
Ali.Z 25 Apr 2020, 17:11
Stefx wrote:
Thank you for your answer
I watched all your material, but unfortunately I have a problem with understanding such instructions as db, mov, push, call, edx, ecx and so on.
What are these instructions for, what do they do, and what can I do with them?


db - define byte - 8-bits
dw - define word - 16-bit
dd - define dword (double word) - 32-bit
and more ...

mov - move data from one place to another
push - saves a value on "the" stack - "the" when referring to the current stack
call - calls a function usually paired with ret instruction to return back to the caller

eax,ecx,edx,ebx,esp,ebp,esi,edi
registers, treat them as containers.
eip is also a register, but its used internally by the processor.

you can find much more about these stuff from your processor manufacturer's manual.

but i do not recommend opening the manuals, since they are not intended for learning from scratch.

instead, refer to fasm documentation it can be useful for people who want to learn.

https://flatassembler.net/docs.php?article=manual

_________________
Asm For Wise Humans
Post 25 Apr 2020, 17:11
View user's profile Send private message Reply with quote
Stefx



Joined: 24 Apr 2020
Posts: 21
Stefx 25 Apr 2020, 17:13
Thank you for your quick replies.
I will look around and I hope I can find what I am looking for.
Post 25 Apr 2020, 17:13
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 26 Apr 2020, 08:06
Stefx wrote:
I watched all your material, but unfortunately I have a problem with understanding such instructions as db, mov, push, call, edx, ecx and so on.
What are these instructions for, what do they do, and what can I do with them?
Could you be more specific where in my tutorials you became lost? Any feedback is going to be helpful for me if I record more in future.
Post 26 Apr 2020, 08:06
View user's profile Send private message Visit poster's website Reply with quote
Stefx



Joined: 24 Apr 2020
Posts: 21
Stefx 30 Apr 2020, 08:45
@Tomasz Grysztar
Sorry for my late answer
It's hard for me to understand the whole guide because, too few concepts are explained.
For beginners it is very important to explain everything.

At the beginning I will note that everything what I will write belowe is only my own view, based on learning c++ language.
Now I will write a few general issues that are worth discussing in the beginner's guide:

1) How should look body of our program (beginning and ending)?
C++: int main(){code here}
2) How look variables to store data, are these registers in .asm?
C++: int, double, char
3) How print any text in console?
C++: std::cout << "text"
4) Basic arithmetic operations (+, -, *, /, %) are the same symbol in .asm?
Why I ask?
In on of russian tutorial I found this code and if there was no comment I wouldn't know this is subtraction
Code:
cmp eax, 45              ;-
       jne notSub
       mov ecx, [A]
       sub ecx, [B]
       push ecx
       push resStr
       call [printf]
       jmp finish    

5) Are the checking conditions stored in the same way as in c++?
C++: if(), else
6) The same question from 5) but about loops
C++: do, while, for
7) To run any kind of basic program will I need any basic libraries? If yes what kind and how can I attach to program?
C++: #include <iostream>
8 ) In assembler exist arrays, references and pointer? If yes how they are saved?
C++: [], &, *
9) In my opinion work with debugger should be discussed when beginners will know basic from above

Finally, forgive me for comparing C++ with assembler, in my opinion every new person who tries to learn assembler, before will have contact with a high level language and it will be difficult for her to understand the differences without explaining based on a high level language.
Post 30 Apr 2020, 08:45
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 30 Apr 2020, 09:21
I think that you are asking questions that would only make sense when learning a modern high level language, and in case of the assembly language they may be a bit misguided.

1) The concept of a "body" of a program is something that is specific just to some HLLs (and even among them, not every one). The assembly at its basic function just creates a sequence of machine code instructions that is going to be put somewhere in memory. Whether there is some additional syntactic package around that, it depends on the specific assembler used. With fasm you can limit the red tape to bare minimum - and this is what I've done in my tutorial, in the beginning I just use "format PE" clause to tell fasm that I want to load and execute some machine instructions in Windows, and that's all. But note that this "format PE" is a syntax specific to fasm and no other assembler supports it. What should be the most important for you is to focus on the actual CPU instructions that get executed, and what they do.

2) For CPU everything is just a memory. You can execute a part of memory as a sequence of instructions, but you can also read it and write into it as just a sequence of bits/bytes. Therefore everything you put into memory can be a variable. In assembly you often define data with directives like DB (data bytes), but what this directive really does is just put some byte values into your file (that then gets loaded into memory to be executed). Assembly instructions that you write also cause some byte values to be generated - the bytes that are their machine codes. And all these bytes, both the ones for code and for data, are somewhere in memory, each one has a consecutive address, and you can access these bytes through the addresses. This is not a HLL where language expects you to have a cell of memory dedicated to store, for example, an integer, and disallows you to use it any other way. With assembly, you can use the same address to store anything you want, or you can cause CPU to execute instructions at that address, etc.

3) Printing text in console might be something that is a basic entry point when learning a new HLL, but not in case of machine languages - because this is not a basic operation. The CPU instructions just move around values in memory, performing various operations on them. If there is an area of memory that is read by some external device to show on the screen (like on many simple old computers, or classic PC with DOS) then that might be relatively straightforward. But in modern systems like Windows or Linux, the console is hidden behind layers of data structures of operating system, and you first need to learn how to communicate with it. This is not simple, it requires learning things like calling conventions first (and this is why I try explaining them quite early in the tutorial series).

Because of that in the beginning it is easier to learn what the instructions of CPU do by just looking at the values in memory with an additional tool, like a debugger. This is why I chose to use debugger all the time in the tutorial.

4) There is a difference between arithmetic operations as performed by assembler to compute some values for you, for example when you write "mov eax,2+2", and the CPU instructions that perform arithmetic operations while your code is running, like "add eax,2". The former is just to make your life easier by the assembler, so that you do not have to write "mov eax,00000004h" and you can express the same number in a different way. The latter is an actual operation that your program does when executed.

5) There are no things like IF/ELSE in the machine code. There are conditional instructions, and they are driven by flags kept in a special register of CPU, and you can affect these flags only in very specific ways. This is covered later in the tutorial, it is not a basic thing.

6) The loops are usually done with jump instructions. If you know any HLL that has GOTO, it is something like that. Also covered a bit later, because to make a loop that is not endless, you need to make some conditional jumps, and that requires knowing how to work with flags, etc.

7) You can execute some machine instructions without needing any additional support - as done in the first parts of my tutorial. But when you want to interact with OS, you need to be able to communicate with it, and this requires various kinds of additional setup - depending on the OS. I show how to link to OS API after explaining the calling conventions.

8) These are all HLL concepts. In machine language everything is just bits/bytes in memory. Every byte in memory has an address, which itself is just a number consisting of bits (32 or 64 bits on modern machines). You can store this number in memory and this way you have something like a pointer (so at one address you have stored a number which itself is an address of something else in memory). As for they array, it is just a sequence of bytes, like everything else. This should be underlined: from the point of view of CPU everything is just sequences of bits/bytes - it is up to you to decide what to do with them.

9) As noted in the point 3 above, debugger is the only way to actually be able to see what CPU does until you learn enough of the OS interfaces etc. to be able to make your program actually show something on its own. Think of it as writing a program for a microprocessor that is only able to send some electrical signals to the outside world. To know what you program does, you'd need to attach some kind of voltage meter (or even oscilloscope) to that processor - and here debugger serves a similar role for us.
Post 30 Apr 2020, 09:21
View user's profile Send private message Visit poster's website Reply with quote
Stefx



Joined: 24 Apr 2020
Posts: 21
Stefx 01 May 2020, 06:08
Thanks for explanation.
So difference betwean LLL and HLL is like "Mariana Trench".
I think it will be hard for me to switch but I will try and will not give up so easily Cool
Post 01 May 2020, 06:08
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20338
Location: In your JS exploiting you and your system
revolution 01 May 2020, 06:27
Stefx wrote:
So difference betwean LLL and HLL is like "Mariana Trench".
Yeah. All the HLL concepts like OO, const, static, etc. don't apply. Although one that might apply would be volatile. In assembly everything is volatile. Change variables however you please, the CPU won't care. And if you mark the code section as writeable then you can also change your code at runtime, aka self modifying code.

One huge advantage is access to the flags. Great for multi-precision arithmetic and detecting overflows.
Post 01 May 2020, 06:27
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 01 May 2020, 12:34
revolution wrote:
All the HLL concepts like OO, const, static, etc. don't apply.

Some of them do apply, but later. OO is not about syntax sugar but about architectural decisions mostly. Besides, COM interfaces are still OO stuff and they are easily accessible from asm, they just turn out not to be an atomic concept.
Post 01 May 2020, 12:34
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20338
Location: In your JS exploiting you and your system
revolution 01 May 2020, 13:07
DimonSoft wrote:
revolution wrote:
All the HLL concepts like OO, const, static, etc. don't apply.

Some of them do apply, but later. OO is not about syntax sugar but about architectural decisions mostly. Besides, COM interfaces are still OO stuff and they are easily accessible from asm, they just turn out not to be an atomic concept.
You are right, but I don't think of them as assembly. They are high level design concepts only, not enforced by the CPU or the assembler.
Post 01 May 2020, 13:07
View user's profile Send private message Visit poster's website Reply with quote
Stefx



Joined: 24 Apr 2020
Posts: 21
Stefx 09 May 2020, 15:24
I would like ask how can I change the values of most significant bit, but keeping values of less significant bit?


Description:
Filesize: 4.71 KB
Viewed: 23380 Time(s)

Registers.jpg


Post 09 May 2020, 15:24
View user's profile Send private message Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 718
Ali.Z 09 May 2020, 16:23
11110503h h = hex

03 - AL
05 - AH
0503 - AX
11110503 - EAX

there are multiple ways to change the upper word of eax:

- using AND and OR
Code:
and eax,0000FFFFh
or eax,0AAAA0000h    


- using SHL and MOV
Code:
mov dx,ax ; XCHG can be used too
mov ax,0AAAAh
shl eax,10h
mov ax,dx ; XCHG can be used too, OR can be used as well    


- using memory location
Code:
mov [my_memory_location],eax
mov dx,0AAAAh
mov word [my_memory_location+2],dx    


and there are more ways of achieving the same result, sometimes its personal preference and sometimes it depends on what you are doing plus the free available registers to use.

_________________
Asm For Wise Humans
Post 09 May 2020, 16:23
View user's profile Send private message Reply with quote
Stefx



Joined: 24 Apr 2020
Posts: 21
Stefx 09 May 2020, 19:01
Nice explained @Ali.Z
I have few question
1) What is the different betwean (AND and OR ) and when should I use some of them?
2)
Code:
and eax,0000FFFFh    

What role does FFFF play that the values of the least significant bits remain unchanged?
Post 09 May 2020, 19:01
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 799
Location: Russian Federation, Sochi
ProMiNick 09 May 2020, 19:52
usualy or set particular bits in some bitflag

or eax, BIT_N or BIT_M

and used to switch that bits of

and eax, (not BIT_N) and (not BIT_M)

xor used to invert their states

xor eax, BIT_N or BIT_M

other cases:
if thou know that target holds zero thou could use or as short form of mov (but here thou should accept that or affect flags, but mov preserve(untouch) them)

or if thou know that target and source of or instruction had notoverlaped bits - or in this case could be analog of add instruction.

difference of AND & OR in logic:(operand order no matter)

1 AND 1 = 1
ANY AND 0 = 0

ANY OR 1 = 1
0 OR 0 = 0
Post 09 May 2020, 19:52
View user's profile Send private message Send e-mail Reply with quote
Ali.Z



Joined: 08 Jan 2018
Posts: 718
Ali.Z 09 May 2020, 19:57
h = hex
b = binary
d = decimal

register:
AL = BYTE = 8-Bits = 00000000
AH = BYTE = 8-Bits = 00000000
AX = WORD = 16-Bits = 0000000000000000
EAX = DWORD = 32-Bits = 00000000000000000000000000000000

as hexadecimal digit:
0h = 0000b = 0d
1h = 0001b = 1d
2h = 0010b = 2d
3h = 0011b = 3d
4h = 0100b = 4d
5h = 0101b = 5d
6h = 0110b = 6d
7h = 0111b = 7d
8h = 1000b = 8d
9h = 1001b = 9d
Ah = 1010b = 10d
Bh = 1011b = 11d
Ch = 1100b = 12d
Dh = 1101b = 13d
Eh = 1110b = 14d
Fh = 1111b = 15d

where nibble equal to one hexadecimal digit, which is equal to 4-Bits.
as BYTE is 8-Bits, so the hexadecimal representation for 1 BYTE can be 00h, two hex-digits.

1. if you ever built an electronic circuit, then it work the same as the logical gates.
if not however, then consider these examples; AND, OR, XOR, NOT work on bit-level:

true = 1
false = 0

AND perform a bitwise AND operation on the destination operand based on the input given in source operand:
AND evaluates True only if both bits from destination and source are true, False otherwise.

Code:
and 0101b , 0011b
; ...
0101b
and
0011b
====
0001b, because only bit-0 is true, other bits are false    


OR perform a bitwise OR operation on the destination operand based on the input given in source operand:
OR evaluates True only if either bits from destination and source are true, False otherwise.

Code:
or 0101b , 0011b
; ...
0101b
or
0011b
====
0111b, because at least 1-bit have to be set in order to evaluates true    


XOR perform a bitwise XOR operation on the destination operand based on the input given in source operand:
XOR evaluates True only if either bits from destination and source are true BUT not both, False otherwise.

Code:
xor 0101b , 0011b
; ...
0101b
xor
0011b
====
0110b, because bit-0 is false, any BIT XOR'ed by itself returns 0 ... 1 xor 1 results 0    


NOT perform a bitwise NOT operation on the destination operand:
NOT flips each True bit with False, and each False with True.

Code:
not 0101b
; ...
not 0101b
====
1010b, flipped result    





2. as answer 1 explain the bitwise operation at binary level, it should becomes clear that we are MASKing the lower WORD bits of EAX.
i.e. preserving them and clearing the upper half.

Code:
; EAX = 11110503h
and eax,0000FFFFh

;0503h equal to 00000101b 00000011b
;masking with FFFF that is 4 nibbles set to 1, 1111111111111111

0000010100000011b
and
1111111111111111b
==============
0000010100000011b

now we preserved the lower WORD of EAX, remember AND evaluates true only if both corresponding bits are true, false otherwise.

we OR the upper WORD of EAX with AAAAh

; EAX = 00000503h
1010101010101010b ; Ah = 10d
or
0000000000000000 0000010100000011b
============================
1010101010101010 0000010100000011b

'which is' AAAA0503    


as for when to use them, there are many uses for bitwise instructions here are very few uses:
- AND, preserve/clear/test some bits.
- OR, basically fill/set the bits but keep in-mind you may get nondeterministic results if the destination bits are unknown by mean you dont know whether it have some bits set or not.
- XOR toggle/clear values.

can be used for basic arithmetic, advanced math stuff or even encryption and decryption.

_________________
Asm For Wise Humans
Post 09 May 2020, 19:57
View user's profile Send private message Reply with quote
Stefx



Joined: 24 Apr 2020
Posts: 21
Stefx 13 May 2020, 14:28
Thanks for your replays @ProMiNick and @Ali.Z

I got stuck on "Introduction to x86 assembly, part 4" where is speech about calling conventions.

First of what I would like to ask, is each registers have a specific job?
In original code from tutorial part 4
Code:
subroutine:
        push    ebp
        mov     ebx, esp
        sub     esp, 8       

I replaced ebp to eax
Code:
subroutine:
        push    eax
        mov     ebx, esp
        sub     esp, 8       


Program was working good but does using different registers matter?

I would like ask how yours learn of assembly language looked like.
For me to understand one tutorial I need almost one day.
If you have some tips how easy to learn assembly I will be very grateful.
Post 13 May 2020, 14:28
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2, 3  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.