flat assembler
Message board for the users of flat assembler.

flat assembler > Main > [16/32/64] Hello, I have a couple of newbie question

Author
Thread Post new topic Reply to topic
neuron



Joined: 27 Jun 2013
Posts: 14
Location: Beijing, PRC
Hello, everyone.

I'm a newbie on FASM, assembly language and computer programming.
(My purpose is just for fun, hobby. Not the act like hacker.)

Here are my questions.

Q1:
While practice assembly language, i really have to start from 16-bit(DOS) programming to 64-bit programming?

Q2:
Is there a step by step tutorial (or practice) about fasm?
When search this forum, there are 'tons' of tutorial, but some of them are dead link, dos, windows, linux and DIY OS(WOW! amazing) things. It is really confusing me.

Sorry for my POOR english. Cool
Post 07 Aug 2013, 06:38
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
A1: IMO, you have to start from 32bit Linux/Windows programming. Then try 64bit Linux/Windows programming. Then (only if you want to create your own OS) learn real mode DOS programming.

A2: I don't know why all beginners start with asking "step-by-step" tutorials. And actually I don't know what meaning they put in this term.

Just try to download several simple examples (there are several in FASMW package or Fresh IDE package).

Then try to simply compile them and run. This way you will setup your development environment.

After that, start to modify some of the examples in order to work a little bit different.

Do it for more and more complex examples.

Start some small project by yourself.

For all this, you probably must have some general programming knowledge.
Post 07 Aug 2013, 08:02
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
dogman



Joined: 18 Jul 2013
Posts: 114
Choose what hardware and OS you want to learn on first. The computer you use for most things is the best one to start learning assembly on.

After you choose the environment you can start zoning in on the other details and find tutorials for your chosen platform.
Post 07 Aug 2013, 08:51
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1904
> have to start from 16-bit(DOS) programming to 64-bit programming?

NO

> Is there a step by step tutorial (or practice) about fasm?

The examples ... start with Hello War Smile

_________________
Bug Nr.: 12345

Title: Hello World program compiles to 100 KB !!!

Status: Closed: NOT a Bug
Post 07 Aug 2013, 09:59
View user's profile Send private message Reply with quote
TightCoderEx



Joined: 14 Feb 2013
Posts: 58
Location: Alberta
JohnFound wrote:
A1: IMO, you have to start from 32bit Linux/Windows programming. Then try 64bit Linux/Windows programming. Then (only if you want to create your own OS) learn real mode DOS programming.


Sound advice and the reason windows, is that there are a lot of free tools like Ollydbg where you can actually insert code on the fly, run through it and see what is actually happening.

First and most important though, is you need understand what the processor can do in the way of instructions. Moving data to and from memory, calculations and branching on conditions are probably a good place to start.

Let's hypothetically say you need to know if stack pointer is evenly aligned on a 256 byte boundary and if so, subtract 1024 from it. The ESP value is 418743 or [663b7H] or 1100110001110110111 binary.

Code:
        mov     eax, esp
        and     eax, 256
        jnz      NotEven
        sub     esp, 400h
NotEven:
        nop    


Tutorials are a great resource, but only after you know what you're looking at. Here is an example of how in a boot loader move sp to top of conventional memory, fill it with -1's and then set the new stack pointer to it.
Code:
    ; Get TOM segment   
        mov     ax, 64                          ; Point to BIOS DATA AREA
        mov     ds, ax
        mov     ax, [19]                        ; Get number of 1k pages. Probably 27FH
        shl     ax, 6                           ; Convert to segment address
        mov     es, ax

 Original has a bunch of code in here to read E820 map.

    ; Build a stack frame of 65k and fill it with -1's so it can be probed for how deep
    ; BIOS routines or my code penetrates this frame
    
        mov     cx, 0x1000
        mov     ax, es
        sub     ax, cx                  ; Bump segment pointer back 65k
        mov     es, ax
        xor     di, di
        shl     cx, 2
        or      eax, -1
        rep     stosd                   ; Fill 32,768 words with - 1
        
    ; Now we can move previously saved 18 words pointed to by SS:SP to this new frame.
    
        cli
        inc     eax
        mov     ax, ss
        mov     ds, ax
        push    es
        pop     ss                      ; Set new TOS (top of stack)
        xor     sp, sp
        sti
    
Even if your not that experienced, but know the instruction set well, you'd be able to spot right away the code that doesn't even need to be there to accomplish job. This is a more detailed an elaborate example, but keep in mind I did a lot of things that don't need to be there. My hobby is algorithm design so I dream up some crazy stuff just to see if I can make it work.
Post 07 Aug 2013, 16:15
View user's profile Send private message Visit poster's website Reply with quote
neuron



Joined: 27 Jun 2013
Posts: 14
Location: Beijing, PRC
To JohnFound.
Thank you. I found 64-bit FASMW in this forum. I'll use that.

To dogman.
Quote:

The computer you use for most things is the best one to start learning assembly on.

OK. I got it.
Thanks!

To DOS386.
Ooops, I think what i need is say hello to this peaceful world.
Thank you for your reply.

To TightCoderEx.
Sorry, I definitely dont know what are your codes saying.
And your link is not accessible in china mainland. Confused
Post 08 Aug 2013, 06:07
View user's profile Send private message Reply with quote
whakamaru



Joined: 03 Oct 2012
Posts: 20
Location: New Zealand
In fasm-for-dos, there is an example USEDPMI.ASM. If I replace the lines between start: and done: with my solution to Euler #5, it compiles, runs and displays the correct answer.
When I put the same lines after the directives...
format MZ
entry main:start
segment main use32
start: nop... etc ...... it compiles OK but doesn't run. I get an error message from "16-bit MS-DOS sub-system"
I assume (at the risk of being half wrong?) that the directives are incorrect. I have studied the documentation and all the examples, but unfortunately there are no examples specifically about 32-bit stand-alone programs.
What might the correct directives be?

Many Euler problems involve HUGE numbers. It is hard trying to solve in 16-bit, and since all modern laptops now have 64-bit CPUs I would like to have a go at 32 or 64-bit solutions.ie, for #401 !
So, what would the directives for a straightforward, relatively short, 64-bit listing that does number crunching and then displays the (correct) answer be?

I have also downloaded FASM for Windows and put the #5 lines into the PEDEMO.ASM example, and much to my surprise it compiled, ran and gave me the correct answer. So maybe a similar insertion into PE64DEMO.ASM might work?

_________________
watch this space
Post 30 Dec 2013, 21:23
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 16651
Location: In your JS exploiting you and your system
You can't use "use32" in a 16-bit program unless you switch the CPU to 32-bit mode before executing the 32-bit portion of the code.
Post 31 Dec 2013, 00:15
View user's profile Send private message Visit poster's website Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1904
> but unfortunately there are no examples specifically about 32-bit stand-alone programs.

Get the Win32 version of FASM. 16-bit DOS alone cannot run "32-bit stand-alone programs".

See also: http://board.flatassembler.net/topic.php?t=6633

and the FAQ http://board.flatassembler.net/topic.php?t=9473 E. 13. "Can I develop 32-bit apps for a 16-bit DOS ?"

and the other FAQ's ... 99.99% of possible problems already covered Wink
Post 01 Jan 2014, 09:18
View user's profile Send private message Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 340
Location: Asia, Singapore
Hi,
Firstly welcome Cool.
Answer 1:
32 bit is recommended. However before starting with anything I'd tell you some basic stuff.
1. In assembly unlike other languages things are controlled by some variables that can be understood
by the CPU, technically called registers.
There are some general purpose registers colloquially GPR's means you can use them for almost anything.
They are :
AX (accumulator)
BX (base)
CX (counter)
DX (data)
SI (source index)
DI (destination)
SP (stack pointer)
BP (stack base pointer)
Don't worry about what's in those brackets, but you can use them for almost anything like storing values
etc. These are 16 bit registers and can holds max value of 65535 (64KB).
To convert them to 32 bit we simply add an E, ex : EAX,EBX,ECX these can hold
up to 2,147,483,647. And to 64 bit you add an R like RAX,RBX,RCX etc.
2. Some instructions.
There are some simple instructions that can be used in assembly language,
Some really common ones are:
MOV, ADD, SUB, DIV, MUL, LOOP, INT, CALL, JMP, JC, STI, CLI, RET, IRET, RETF, PUSHA, POPA, PUSH, POP, PUSHF, POPF.
Check out on the internet or the FASM Manuals what these mean, they aren't that complex.
3. Get familiar with hexadecimal and binary systems,
Basically hexadecimal system is base 16 as opposed to the normal base 10 system.
In normal counting numbers are:
1 2 3 4 5 6 7 8 9 10
In hexadecimal there are 16 instead of ten.
1 2 3 4 5 6 7 8 9 A B C D E
A simple conversion would be:
10 = 0xA
Note: We add a 0x in front of a hexadecimal number to denote that it has a hexadecimal value, another notation is adding a 'h' add then end like A000h,
FASM accepts both notations so you can use whatever you want.
4. Finally I'd say the main difference between 16/32/64 bit is the way you access
memory, in 16 bit you can't access more than 64KB at a time unless you mess
with ugly segment registers which are a big nightmare and even using that you will only be able to access 1MB, so writing in 16 bit mode means that you must write small and efficient code which I at least can't write. In 32 bit I think you can access up to 4GB of memory, if you want to access more then there is something called PAE, which isn't as horrible as segment registers, then in 64 bit theoretically you can access up to...... well 4PB (THEORETICALLY!)
Answer to question 2
Tutorial? There are a lot of examples in the FASM/EXAMPLES directory that comes with FASM, they show simple concepts like drawing message boxes, text editing, OpenGL (not sure about this one but I think I saw it).
Cheers,
sid123
Post 07 Jan 2014, 09:13
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1904
In 32-bit code you should use 32-bit registers, 16-bit (AX,BP,...) is mostly possible but inefficient (8-bit is OK), 64-bit (RAX) is NOT possible.
Post 08 Jan 2014, 08:54
View user's profile Send private message Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 340
Location: Asia, Singapore
Yes using 32 bit registers in 16 bit is also possible but I guess there is a limit prefix (66h?) that's added. 16 bit code in 32 bit is possible through Virtual 8086 Mode, In 64 bit this is not possible unless you use an emulator.
Post 08 Jan 2014, 09:26
View user's profile Send private message Reply with quote
whakamaru



Joined: 03 Oct 2012
Posts: 20
Location: New Zealand
thanks...especially DOS386... such a lot to learn... much on these pages...I'll get there... maybe...
Post 13 Jan 2014, 21:54
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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-2019, Tomasz Grysztar.

Powered by rwasa.