Professional Documents
Culture Documents
Asm 1
Asm 1
– variable declarations
– array subscripts
– assignment statements
– if/else statements
– while loops
assembler is harder and slower to read and write ing MMX instructions
assembler is not portable. and AL for computing 8-bit results. Do not use other
registers for computations.
The only things that should be written in assem- Use BX only for computing addresses (indexed ad-
bler are: dressing), and DX only for IN and OUT instructions.
Save the result of each expression at the end of
code that is not possible to write in a high-level each statement – do not try to save a value across
language such as the code that does a context two statements by leaving it in AX or AL.
save/restore (or context switch) before/after an All labels should be unique. Functions should
interrupt have meaningful labels but targets of short branches
asm1.tex 1
within a function may assigned non-meaningful la- Assignment Statement Template
bels. For example, the labels in the templates below
A simple assignment statement of the form:
are of the form ln.
z = x op y ;
Notation
can written as follows in assembler:
The notation op below refers to an arithmetic or log-
ical operator. For example, use ADD for +, SUB for -, mov ax,x
AND for &, OR for |, etc.) op ax,y
mov z,ax
The notation cop below is used for a compari-
son operator while cop* is its complement. The
For example example, the C expression:
following table shows the C comparison operators
and their complements along with the conditional c = a - b ;
branch instruction to be used. The signed version is
used when the variables being compared are in two’s- involving byte variables is written as follows in as-
complement (C signed variables). sembler:
mov al,a
assembly assembly
sub al,b
C C signed unsigned
mov c,al
op op* op op
> <= JG JA
and the C expression:
< >= JL JB
<= > JLE JBE e = d & 0x1000 ;
>= < JGE JAE
== != JE JE is written as follows:
!= == JNE JNE
mov ax,d
The notation s1... stands for other statements (of and ax,01000H
any type). mov e,ax
2
if/else Statement Template while Statement Template
The C code: A C while loop:
if ( a > b ) { s1...
c = 0 ;
} else { i = i + 1 ;
c = 1 ; }
}
which is written as follows in assembler:
is written as follows in assembler: mov ax,0
mov i,ax
mov al,a
cmp al,b l1: mov ax,i
jle l3 cmp ax,n
jge l2
mov ax,0
mov c,ax s1...