• Log in or sign up for an account here: https://www.sythe.org/login

Good asm intro

DatSik.

Active Member
Joined
Aug 16, 2012
Sythe Gold
0
Messages
183
Age
36
Location
Oklahoma
No credits to me, found on web

Day 1




x86 Assembly ?


Yep, Assembly Language on the PC. More specifically, 16bit Assembly using BIOS
and MS-DOS. I'll assume that you are using A86, which you can get here.

Some Assumptions


In addition to the A86 Assembler, I assume that you either are good at learning
new instructions/syntax from context of other things. It wouldn't hurt to know ASM on another
platform either.

The x86 Processors


The x86 processors are a group that run anywhere from 10-3000 MHz (Note: That
I include the Pentium chips in these.). They have much less CPU registers than CPUs used
in other systems (such as game consoles), the registers are:

AX - Accumulator, implicitly used in some math instructions.
BX - Base register, also used in some math instructions.
CX - Count register, used implicitly with the Loop instruction.
DX - Data register, also used in some math instructions.
CS - Points to the current memory segment that is currently being executed.
DS - Points to the data segment of the program (same as CS for us.)
SS - Points to the stack segment of the program (")
ES - A scratch segment pointer.
BP - Points to the base of the built-in CPU stack.
SP - Points to the top of the built-in CPU stack.
IP - Points to the location of the currently executing statement (can't explicitly access IP).

All registers can really be used by us for anything, except for certain instructions leave
their results in certain registers. Also, IP you can't do anything to with code explicitly, the only
way to change it is with JUMPing instructions, and you can't read it either.

Registers and Numbers


The single most important instruction is MOV (for MOVe), MOV moves anything between registers.
Here's some examples:

mov ax,3 ; Anything that follows a ';' is a comment and is ignored by the assembler. This is AX = 3.
mov ax,bx ; AX = BX;
mov ax,[bx] ; AX = *BX; if you know C, if you don't, go away before I hit you with a trout.
; More on ^ this later.

I hope you get the idea by now, however, there's one small thing you should know:

mov es,3 ; you can't just put a number into a segment register, you have to do it like this:

mov ax,3
mov es,ax ; you don't have to use AX, of course, you could use any. (AX,BX,CX,DX).

The Stack


I'll assume you know what a stack is, you put a number at the beginning, (a push), and then
when you're done putting numbers ontop of one another, you start taking them off the top (a pop). So
when you finish they are in reverse order. (If you know what a stack is, skip to next paragraph). Image
a stack of lunch trays, you put one on, put another on. The first one to go on, is the last one to come
off. FILO (First In Last Out) is the semi-techinical word/description for a stack.

The 2 most important instructions for the Stack (quite well named, in fact) are PUSH and POP,
examples:
push ax ; AX onto stack
push dx ; DX onto stack

pop bx ; pop into BX
pop cx ; pop into CX

This Day In Review



Tomorrow, we'll get to assembling a program that actually gives output!

Happy coding,
-Mike H a.k.a GbaGuy



Day 2




Interrupts


Interrupts are exactly like they sound, they interrupt your program, do their thing, and
then return to where your program left off. Unlike game consoles, interrupts on the IBM-PC compatibles
are actually called by your program like an API or static library.

The INT Instruction


The INT instruction calls an interrupt, it works like the following:

int 21h ; a 'h' after a number means hexadecimal notation.
; 21h is the number for a DOS Interrupt Service routine.
; there are many functions that can be called with interrupt 21h

The number that follows the INT instruction (21h in the above example) is the service to
call. The actual function number goes in AH, which brings me to something rather important:

I've said that all registers are 16bit, that's true except for one thing:
The High and Low 8bits of AX,BX,CX,and DX can be accessed by substituting the X for
either H for High and L for Low, so the lower 8bits of the registers are AL,BL,CL,and DL, and the high
8bits are AH,BH,CH,DH. The MOV instruction works the same for accessing the 8bit parts of the registers
as well as the registers as their 16bit whole selves.

Please note that if you touch AH or AL for example, you'll be modifying AX's value. So watch out.

A Character on the Screen


Here, we'll put together a program that outputs a letter to the screen and assemble it to the
final COM program (a DOS EXE,sort of).

;;---CODE START---;;
mov ah,2 ; 2 is the function number of output char in the DOS Services.
mov dl,'E' ; DL takes the letter to be outputted. note that "mov dl,'E'" will put 'E's ASCII code in DL.
int 21h ; will call DOS Services, DOS will notice that the function # is 2 and will then take the ASCII code
; in DL and output it to the screen at the currect cursor location.

mov ah,4Ch ; 4Ch is the function number for exit program in DOS Services.
int 21h ; function 4Ch doesn't care about anything in the registers.
;;---CODE END---;;

Put that in a file called first.asm, and open a DOS Prompt, type:

a86 first.asm Hit ENTER

It should output 2 files, keep the COM file and delete the other. Open a new DOS Prompt if you closed the old one.
Type:

first Always hit enter after every line

A capital E should be outputted and the program should end.

This Day In Review



Tomorrow, we'll get to see pointers in action!

Hope you understood everything!,
-Mike H a.k.a GbaGuy


Day 3




Pointers


Pointers in ASM are really just memory addresses.

Memory layout for 16bit DOS COM


Memory in 16bit DOS is segmented into 65,536 65,536 byte segments accessed like so:

0001:0800h ; segment:offset (in bytes) , usually hex even if you don't see an 'h'.

A program (the way we're doing it) is limited to the space inside of one segment, much later, perhaps
we will assemble to an object file and link to an unlimited, multisegment EXE file.

A pointer is usually accessed with a segment register holding the segment and either an explicit number
or BX or DX holding the offset.

"Variables"


Variables in ASM are just labels for memory addresses. You also want to jump over variable
declarations, but we'll learn jumps later.

Here's some code:

var1 dw 0 ; var1 labels the currect 16bits location in memory.

Note that if you do:

mov dx,30 ; just something to make my point
k db 90h ; (db is define byte), 90h is machine code for a do nothing instructin (NOP).
mov ax,4 ; The 90h will be run by the CPU as an instruction before the MOV happens.

That's to show you that these are just labels for memory and not special things like in High Level Languages.

Making a Pointer


Here's the code:

var1 dw 0
mov bx,offset var1 (the offset operator gives the offset part of the address)

now with that, we could just use [bx] to access what is at the address in BX, because everything that
we're using should be in our single segment.

Other times that's not so good, sometimes we need to get the segment also. The segment of the currently
running code is always in CS. DON'T EVER CHANGE CS!!, but you can read CS like so:

mov ds,cs ; put CS's value into DS.

Accessing the Pointer


Here's code:

var dw 0
mov bx,offset var ; give BX var's address
mov [bx],6 ; put 6 into the address in BX

Note that only Bx and DX can be used as offset pointers. If you had a segment also you needed to use then
it would go like this: (assuming the segment was in ES)

mov [es:bx],6 ; put 6 into address at segment ES with offset BX.

A better way for variables


If you have just a simple number to put into/or read from a variable, then there's an easier way to accomplish
it. Just:

var dw 0
mov ax,7
mov var,ax ; I think you have to use a variable, you can't just put a number in. not sure...
; that will put AX's value into var.
mov dx,var ; will take var's value into DX.

Easy, right?

This Day In Review



Tomorrow, some math instructions!

Until Next Time!,
-GbaGuy
 
This is horrible. It makes one reference to an Assembler, and doesn't give you any other hints to what you need. Plus, it doesn't really explain anything very well.

1/10 would not follow.
 
The title stated intro to asm, not intro to asm programming.... ...... ....
 
Runescape Gamblingrunescape gambling
Back
Top