flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Windows > Learning Assembly

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
Trinitek



Joined: 06 Nov 2011
Posts: 256
Post 01 Apr 2017, 00:55
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2122
Location: Usono (aka, USA)

C0deHer3tic wrote:
I looked in the Pcasm book and could not find the movsx or movzx command. However I found this ...



I already pointed you to old NASM 0.98.39's docs, which has a fairly good basic instruction list. It's certainly simpler than Intel's or AMD's docs.


Code:

Archive:  nasm-0.98.39-xdoc.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
   565153  01/15/2005 16:21   doc/nasmdoc.txt
---------                     -------
   565153                     1 file





Quote:

B.4.181 `MOVSX', `MOVZX': Move Data with Sign or Zero Extend

MOVSX reg16,r/m8 ; o16 0F BE /r [386]
MOVSX reg32,r/m8 ; o32 0F BE /r [386]
MOVSX reg32,r/m16 ; o32 0F BF /r [386]

MOVZX reg16,r/m8 ; o16 0F B6 /r [386]
MOVZX reg32,r/m8 ; o32 0F B6 /r [386]
MOVZX reg32,r/m16 ; o32 0F B7 /r [386]

`MOVSX' sign-extends its source (second) operand to the length of
its destination (first) operand, and copies the result into the
destination operand. `MOVZX' does the same, but zero-extends rather
than sign-extending.



BTW, I also think (aforementioned) Ray Seyfarth's EBE (free on SF.net) would help you debug such programs (see the tutorial). Maybe everyone here disagrees (or has a better suggestion, OllyDbg?), but I really think it would be more helpful than trying to understand manually.
Post 01 Apr 2017, 01:19
View user's profile Send private message Visit poster's website Reply with quote
Trinitek



Joined: 06 Nov 2011
Posts: 256
Can't go wrong with OllyDbg. I'm starting to warm up to x64dbg, which looks similar and is under active development.
Post 01 Apr 2017, 01:34
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 556
Yeah, too bad x64dbg is so stupidly bloated compared to OllyDbg. I mean, orders of magnitude more bloated... (but OllyDbg 64 seems to be dead, sigh)
Post 01 Apr 2017, 11:11
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6435
Location: Kraków, Poland

Furs wrote:
Yeah, too bad x64dbg is so stupidly bloated compared to OllyDbg. I mean, orders of magnitude more bloated... (but OllyDbg 64 seems to be dead, sigh)

If you'd prefer something on the opposite end of spectrum of "bloat", you can find it on this very board.
Post 01 Apr 2017, 11:58
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 556
Oh wow very interesting, thanks Smile
Post 01 Apr 2017, 15:13
View user's profile Send private message Reply with quote
C0deHer3tic



Joined: 25 Mar 2017
Posts: 49

revolution wrote:
The code I posted above from the fasm sources will print to the console without printf. It uses GetStdHandle and WriteFile. So you can too.



I have imported the libraries, and I am still unable to go on from there. I am feeling rather dumb. It is requiring [character], but I have no idea how to use the code.
No disrespect, but I feel like everything is all scattered, at least for me it is. This is mainly my fault, seeing that I am not knowing the right questions to ask. I am, and will diligently continue as best as I can. I am learning after all.



revolution wrote:
And your code for the looping example pushes ebx each time around the loop. But no associated pops are present. You will overflow the stack if you do that too many times.


I need to study more on the push and pop.


revolution wrote:

And note that the dec instruction also sets the Z flag so you don't need the following cmp.


If I take away the cmp ebx,0 it fails to print out the numbers, like so.

Code:

7
6
5
4
3
2
1
0
Enter your name:



Instead it prints just:

Code:

7
Enter your name:



Which I don't want. However I know you are more experienced in this, so you must be right. Therefore, how would I be able to get the full number output with no cmp?

@Trinitek,
I have got that pdf and it is very useful. Thank you for the recommendation.

OllyDbg I have. Very informative indeed. Makes me think of IDA pro in some ways.

_________________
- Just because something is taught one way, does not mean there is not a different way, possibly more efficient. -
Post 02 Apr 2017, 05:07
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 556
Show your full (modified) code? The cmp is indeed useless but you probably messed up something else.

BTW "push" is very easy to understand.

In C, when you put something in an array via a pointer, you do something like this:


Code:
*ptr++ = blah;

But in this case it's in reverse, so instead of 'ptr' we have 'esp' register, so it is like this pseudo C-code:

Code:
*(--esp) = blah; // same as "push blah"

Where 'esp' is a pointer to a 4-byte data (i.e. int).

So it simply decrements esp by 4, then puts the value in the "push". Pop does the opposite: loads the value at "esp", and then increments it:

Code:
// pop blah
blah = *esp;
esp++;

Post 02 Apr 2017, 11:30
View user's profile Send private message Reply with quote
C0deHer3tic



Joined: 25 Mar 2017
Posts: 49

Furs wrote:
Show your full (modified) code? The cmp is indeed useless but you probably messed up something else.



Code:

format PE console
entry main
include 'macro/import32.inc'

section '.data' data readable writeable
msg3 db "%d",1010b,0
msg db "Enter your name: ",0

section '.code' code readable executable

main:
mov ebx8
loop_1:
                dec ebx
                push ebx
                push msg3
                call [printf]; Still using printf. Have not figured out the other one.
                ;cmp ebx, 0
                jnz loop_1
push msg
call [printf]
pop ebx
mov dword [esp],0
push 0
call [exit]

section '.idata' import data readable
library msvcrt,'msvcrt.dll'

import msvcrt,\
printf,'printf',\
exit,'exit'




This doesn't print out the numbers as it goes. It just does:

Code:

7
Enter your name



_________________
- Just because something is taught one way, does not mean there is not a different way, possibly more efficient. -
Post 02 Apr 2017, 19:59
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
Re: Learning Assembly

C0deHer3tic wrote:
What is the assembly equivalent of printf?



Funny question for assembly programmers. I will try to keep it philosophic. Printf is an equivalent of maybe a few hundred or more assembly instructions in a special composition. Wink

Assembly instruction is the smallest part of any task. If you read a book, than the whole book can be imagined as an application while the chapters are functions inside the application and words may be treated as c-functions while the letters contained of any word are assembly instructions building the c-function.
Post 02 Apr 2017, 21:47
View user's profile Send private message Send e-mail Reply with quote
C0deHer3tic



Joined: 25 Mar 2017
Posts: 49
Re: Learning Assembly

shutdownall wrote:

C0deHer3tic wrote:
What is the assembly equivalent of printf?



Funny question for assembly programmers. I will try to keep it philosophic. Printf is an equivalent of maybe a few hundred or more assembly instructions in a special composition. Wink

Assembly instruction is the smallest part of any task. If you read a book, than the whole book can be imagined as an application while the chapters are functions inside the application and words may be treated as c-functions while the letters contained of any word are assembly instructions building the c-function.



Thank you for explaining that.

I am new to assembly, and therefore I have no idea where to start. I tried to start off like learning C, however I see that I needed to know other things before that. I am a noob to all of this, so my questions will seem ridiculous I am sure.

_________________
- Just because something is taught one way, does not mean there is not a different way, possibly more efficient. -
Post 02 Apr 2017, 22:19
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
The main thing is, that there is no equivalent of assembly instructions to c-functions like printf because assembly instructions are a part of these functions. There can not be an equivalent except many instructions carefully composed to a function similar behaving as the c-function you desire.

Or talking in chemistry: An assembly instruction can be imagined as an element / atom and you are trying to find an equivalent of a complex molecule with just a simple atom.

Best start would be tutorials in the internet for assembly programming x86. A more easy step is to program WIN applications using the WIN functions, which can be called simply using register values and pointers and using data structures. I think this is the fastest way keeping motivation while showing fast success of your work.

Try out the Hello World example and then get deeper step-by-step. There are many examples provided with FASM as well which can be compiled fast and investigated after in detail.
Post 02 Apr 2017, 22:39
View user's profile Send private message Send e-mail Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 473
Location: Belarus
I've been visiting this topic from time to time and frankly speaking I don't understand all that "I am new to assembly, and therefore I have no idea where to start" stuff. You have all the Internet at your fingertips. Why don't you find a book and go through it step by step learning registers, then addressing, then how to call functions, and so on and so forth? I remember when I first started to learn assembly, I didn't have Internet connection at all. I bought a book at the store and slowly read it page by page, trying understand, trying to assemble examples inside.
Debugger is a great thing -- you don't even need full executable to check what this or that instruction does. You can just type instructions and execute step-by-step and see what happens. A very convenient thing I didn't have in the beginning. Well, soon I had debug.exe, but that's no comparison what you've got now (mentioned here xdbg or olly)... I don't know...
BTW I still have that book https://drive.google.com/file/d/1sWaYD0Zb_g4RvubiWoeRhMaf77VO-YNAWQ/view[/url]
Post 02 Apr 2017, 23:30
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14924
Location: 6EQUJ5

C0deHer3tic wrote:
This doesn't print out the numbers as it goes. It just does:

Code:

7
Enter your name



You added a call in the middle so now the flags will not be valid anymore. If you moved the dec instruction to after the call, rather than before, then it will be fine.
Post 03 Apr 2017, 00:33
View user's profile Send private message Visit poster's website Reply with quote
Trinitek



Joined: 06 Nov 2011
Posts: 256

C0deHer3tic wrote:
I am new to assembly, and therefore I have no idea where to start. I tried to start off like learning C, however I see that I needed to know other things before that. I am a noob to all of this, so my questions will seem ridiculous I am sure.

But you've already started, haven't you? You're already on your second thread and asking the "right" questions, as some might say.

I say you should refocus on an end goal, not just "learning assembly." Can you think of a particular project you want to tackle as an exercise? Perhaps something involving a simple algorithm, even involving floating point if that's your fancy. (That would be a good entry to SSE or x87) Or maybe you want to take a look at stack operations first?
Post 03 Apr 2017, 00:35
View user's profile Send private message Reply with quote
C0deHer3tic



Joined: 25 Mar 2017
Posts: 49

shutdownall wrote:
Best start would be tutorials in the internet for assembly programming x86.


I tried starting with the x86 programming. It led me down roads where the code would not run on my computer, unless usinging DosBox, or emulators. Trust me, I have looked. That does not mean it is not there, I just have not found it, and so I came to here since I did not find a beginners tutorial for FASM. C is coded the same way in almost every compiler, except for some (Turbo C). I have masm32, and I have FASM, as well as nasm.
1. The tutorial for masm32 was GUI, and I wanted to stay clear from that at the moment.
2. The nasm tutorial had two problems:
A. It could not run in the windows 8 system, when compiled.
B. The other tutorial was for Linux. Thus it used Linux interrupts.
3. FASM was my next go to. And I understand you all must think I am either ignorant or trolling, but I just have been all over the place. I find a tutorial I think will teach me something, and then I am back to square one.
Why would I want to learn programming for code that doesn't even run in my environment? If there is a good reason, I am open to hear it.


Quote:
A more easy step is to program WIN applications using the WIN functions, which can be called simply using register values and pointers and using data structures. I think this is the fastest way keeping motivation while showing fast success of your work.


What do you mean by WIN applications? Are you talking about windows applications? If so, what do you mean by using the WIN functions?


Quote:
Try out the Hello World example and then get deeper step-by-step. There are many examples provided with FASM as well which can be compiled fast and investigated after in detail.



I understand how to print out "Hello world". As far as get deeper and deeper? That is my whole goal here.

Let me link you to a pdf on learning C. This is the type of learning I am looking for.
https://kldp.org/files/c+in+21+days.pdf

And at this point, I would pay for a class to learn assembly. You all are helping me, and I appreciate it.

@zhak,

With all due respect, that book you showed is in Russian and I don't understand Russian. But I hear you. I have tried to read several books.
----------------------------------------------------------------------------------
1.PC Assembly Language - Paul A. Carter.
2. Assembly Language: Step-by-Step - Jeff Duntemann
3. The Art of Assembly Language - Unknown
4. Programming from the Ground Up - Johnathan Barlett (Edited by Dominick Bruno, Jr.)
5. Windows Assembly Programming Tutorial - Jeff Huang
----------------------------------------------------------------------------------
I could not follow along from the simple problem of not being able to compile and run their code. It would either crash, not execute, or spit out errors.

_________________
- Just because something is taught one way, does not mean there is not a different way, possibly more efficient. -
Post 03 Apr 2017, 00:38
View user's profile Send private message Reply with quote
C0deHer3tic



Joined: 25 Mar 2017
Posts: 49
@Trinitek

Okay a goal would make a calculator (text based)

Here is what I need to learn.
1. Output (I know that with printf)
2. Input (I understand this with scanf)
3. Math functions (add, sub, div, etc which I understand)
4. I need a main loop which allows me to run a menu:

Code:

MENU
--------
1. Add
2. Sub
etc...



Therefore I can add, and then get the result and it will take me back to the menu.
5. Also a big help to me, and a code shortcut in C I learned for making lines was this simple command:

Code:

void lines(int amount)
{
    int i;
    for(i=0;i!=amount;i++)
         printf("\n");
    return;
}
...........
So in the code I could do this

printf("Hello World!");
lines(2);



I tried to integrate this function in my assembly program, but I don't know how to pass a variable to a function. I am not even sure how functions work in assembly.
6. Each math operation I would like it to be a function:
int ADD(int y, int x);
int SUB(int y, int x);
etc.....
That is a good place to start, I think. I did the same in C.

_________________
- Just because something is taught one way, does not mean there is not a different way, possibly more efficient. -
Post 03 Apr 2017, 00:50
View user's profile Send private message Reply with quote
C0deHer3tic



Joined: 25 Mar 2017
Posts: 49
Here is the program so far. Only adds right now.


Code:


; Adds two numbers and prints results 
;------------------------------------------------

format PE console
entry main

;IMPORTS
;------------------------------------------------
include 'win32a.inc'
section '.idata' import data readable
library kernel32"kernel32.dll", \
                msvcrt"msvcrt.dll"

import kernel32, \
         ExitProcess"ExitProcess"

import msvcrt, \
         printf"printf", \
         scanf"scanf"
;------------------------------------------------

;DATA
;------------------------------------------------
section 'data' data readable writeable
prompt1         db              "Please enter a number:",9h,0
prompt2         db              "Enter next number:",9h,0

result          db              "The number %d added to %d = %d",0
newline db              "",1010b,0

input           db              "%d",0

num1            dd              ?
num2            dd              ?

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

;[Main Program]
;------------------------------------------------
section '.code' code executable

;[PROGRAM START]
;------------------------------------------------
main:

;[Prints Prompt1 to console]
;------------------------------------------------
                push    prompt1
                call    [printf]
;------------------------------------------------

;[Get input]
;------------------------------------------------
                push    num1
                push    input
                call    [scanf]
;------------------------------------------------

;[Newline]
;------------------------------------------------       
                push    newline
                call    [printf]
;------------------------------------------------

;[Prints Prompt2 to console]
;------------------------------------------------
                push    prompt2
                call    [printf]
;------------------------------------------------

;[Get input2]
;------------------------------------------------
                push    num2
                push    input
                call    [scanf]
;------------------------------------------------

;[result printed out]
;------------------------------------------------
                mov   ebxdword[num1]
                add     ebxdword[num2]
                push    ebx
                push    dword [num2]
                push    dword[num1]
                
                push    result
                call    [printf]
;------------------------------------------------

;[Clean up]
;------------------------------------------------
                add             esp4*4
                push    0
;------------------------------------------------

;[Kill program]
;------------------------------------------------
                call    [ExitProcess]
;------------------------------------------------



_________________
- Just because something is taught one way, does not mean there is not a different way, possibly more efficient. -
Post 03 Apr 2017, 01:02
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2122
Location: Usono (aka, USA)

C0deHer3tic wrote:
Why would I want to learn programming for code that doesn't even run in my environment? If there is a good reason, I am open to hear it.



I have started a new thread (in "Programming Language Design" subforum) as a weak means to address this: why learn code that doesn't run?
Post 03 Apr 2017, 06:44
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< 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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.