P. C. Scipio's Guide to Assembly


Welcome to P. C. Scipio's Guide to Assembly, *THE* first addition to the coding resources page. Basically, I'm going to cover both PC (DOS) Assembly and MIPS (SGI Assembly), most likely focusing on the MIPS, as I'm a little more experienced with it. Anyway, I'm going to start with the basic differences between the two, which is...

CISC vs. RISC

Basically, DOS Assembly is designed for a CISC ( Complex Intruction Set Code) Processor, while MIPS is designed for a RISC (Reduced Instruction Set Code) Processor. What does this mean to you? Good question! What this means is that DOS Assembly is going to have a much larger command set than MIPS will, and, in general you'll have a lot more leway with DOS ASM code than one would have with MIPS. For example, there is a HUGE difference in the amount and kinds of jump or branch instructions in the two langauges. Most MIPS compilers will accept alternative branches and jumps, but translate them to the standard ones opon compilation.

Anyway, the first thing to learn 'bout Assembly is the register naming conventions. The following tables show how the registers are named in both DOS ASM and MIPS :

MIPS - General Purpose Registers

NumberNameDescription
$0zeroAlways Contains 0
$1$atReserved for Internal Assembler
$2,$3$v0,$v1Syscall Return, Expression Evaluation
$4-$7$a0-$a3Function Call and Syscall Parameters
$8-$15$t0-$t7Temporaries, volatile
$16-$23$s0-$s7Temporaries, nonvolatile
$24-$25$t8-$t9Temporaries, volatile
$26,$27$k0,$k1Reserved for Operating System
$28$gpPointer to Global Static Memory
$29$spStack Pointer
$30$fpFrame Pointer
$31$raFunction Call Return Address

DOS ASM - Registers

32-bit (386+)16-bit8-bitDescription
eaxaxahACCUMULATOR - Usually used for basic math, ax and al are the most common area for system call return values. ax is probably the most useful register.
al
ebxbxbhBASE - Also used for a lot of the same operations as ax, bx has far fewer special functions. Returns of 32-bit numbers from system calls are often held in dx:bx with bx as the offset register.
bl
ecxcxchCOUNTER - Also used for a lot of the same operations as ax, cx has fewer special functions. The main special function of cx is the fact that it is used as the loop counter for assembly loop and rep commands.
cl
edxdxdhDATA - Also used for a lot of the same operations as ax, dx has far fewer special functions. Returns of 32-bit numbers from system calls are often held in dx:bx with dx as the base register.
dl
espspSTACK POINTER - one of the most important pointer registers. It points to the offset of the stack in memory, and is therefore a crucial register to know about and use. One can usually find sp being used as the offset register for ss.
ssSTACK SEGMENT - is another crucially important register, as it points to the base of the stack. One can usually find ss being used as the base register for sp.
ebpbpBASE POINTER - yet another pointer register, this one has a more general purpose. Its used for just about anything, although it is commonly used to measure an offset into the stack for some reason of another.
csCODE SEGMENT - points to the location in memory of currently executing code. Most assemblers won't let you alter it... for a *very* good reason!
eipipINSTRUCTION POINTER - yet another pointer register, this one is cs' partner in crime, marking the next instruction to execute. Again, most assemblers won't let you alter it.
dsDATA SEGMENT - Usually used to mark the location of data for some reason or another.
esEXTRA SEGMENT - is one of the main registers used almost exclusively as a pointer. One can usually find es being used as the base register for di.
fsEXTRA SEGMENT - is one of the main registers used almost exclusively as a pointer. I believe it is a later addition to the chip, existing on 386+ machines only.
gsEXTRA SEGMENT - is one of the main registers used almost exclusively as a pointer. I believe it is a later addition to the chip, existing on 386+ machines only.
edidiDESTINATION INDEX - yet another pointer register, di by convention tends to mave a more specific purpose. It tends to be used to mark destination locations in memory.
esisiSOURCE INDEX - yet another pointer register, si by convention tends to mave a more specific purpose. It tends to be used to mark source locations in memory.
eflagsflagsFlags - Each bit of the flags register has an important value. Sarting from the leftmost bit :
0 - Carry Flag
2 - Parity Flag
4 - Auxillary Carry Flag
6 - Zero Flag
7 - Sign Flag
8 - Trap Flag
9 - Interupt Enable Flag
10 - Direction flag
11 - Overflow Flag
12-13 - I/O Protection Level (286+)
14 - Nested Task Flag (286+)
16 - Resume Flag (386+)
17 - Virtual 8086 mode Flag (386+)
18 - Alignment Check Flag (386+)

So what do all these registers mean to you? Well, although the DOS machine is a little different, both MIPS and DOS assembly are dependant on basically a variety of load-and-store architecture. What this means, is that for the most part, you have to load data into registers before performing operations on them. Granted there are exceptions to this in DOS Assembly, but they are fundementally similar. To add two numbers, for example, you have to load them into registers first. Here are examples of simple addtion in both MIPS and DOS Assembly:

DOS ASM MIPS
mov ax, [a]lw $8, a
mov bx, [b]lw $9, b
add ax, ax, bxadd $8 , $8, $9
mov [c], axsw $8, c

With some DOS ASM compilers, including my favorite, Tubo Pascal 7.0, you can cheat around all those operations by doing the following, though I don't believe that this is supported in the basic 8086 command set :

Oh, yeah, and not all the compilers need the brackets either.... in fact most don't. Anyway, the next important thing is the command set. Both DOS ASM and MIPS have a relatively simple basic command set. Then, they both have hoards of other commands that are supported as well, although MIPS' spare commands are often only supported on the compiler not architectural level.

DOS ASM and MIPS Basic Command Set

For the following examples, rx, sx and tx represent DOS ASM registers;$r,$s and $t represent MIPS registers, and in both cases, a, b, and c represent variables in memory. n is a integer constant.

DOS ASM CommandName/Explaination MIPS Command
add rx, sx, txAdd : r = s + tadd $r, $s, $t
and rx, sx, txAND : r = s AND tand $r, $s, $t
call procCALL PROCEDURE procjal proc
dec rxDEC : r = r - 1addi $r, $r, -1
div rx, sx, txDivide : r = s / tdiv $r, $s, $t
inc rxINC : r = r + 1addi $r, $r, 1
je rx, sx, procJUMP if r = sbeq $r, $s, proc
jg rx, sx, procJUMP if r > sbgt $r, $s, proc
jl rx, sx, procJUMP if r < sblt $r, $s, proc
jge rx, sx, procJUMP if r => sbge $r, $s, proc
jle rx, sx, procJUMP if r <= sble $r, $s, proc
jne rx, sx, procJUMP if r <> sbne $r, $s, proc
mov rx, [x]LOAD : r = xlw $r,x
mov rx, sxMOVE : r = smove $r, $s
mul rx, sx, txMultiply : r = s * tmul $r,$s, $t
neg rxNEG : r = -r neg $r
or rx, sx, txOR : r = s OR tor $r, $s, $t
sete rx, sx, txSET r=1 if s = tseq $r, $s, $t
setg rx, sx, txSET r=1 if s > tsgt $r, $s, $t
setl rx, sx, txSET r=1 if s < tslt $r, $s, $t
setge rx, sx, txSET r=1 if s => tsge $r, $s, $t
setle rx, sx, txSET r=1 if s <= tsle $r, $s, proc
setne rx, sx, txSET r=1 if s <> tsne $r, $s, $t
mov [x], rxSTORE : x = rsw $r,x
sub rx, sx, txSubtract : r = s - tsub $r, $s, $t
xor rx, sx, txXOR : r = s XOR txor $r,$s, $t

Well, those are the most important assembly commands, and as I get more time, more and more commands will be posted. As for now, I'll leave you with two sample shells for assembly programs, one for MIPS and another for DOS ASM.

DOS ASMMIPS
.model type
.data

-data-

.code

-insert code here-

.data

-data-

.text

-insert code here-

Page Created and Maintained by P. C. Scipio

Written in HTML

Last Edited on : 3/9/04