Assembly Language Programming
Why learn assembly language
assembly language
S2 hypothetical processor
example
tools
Why
Programmers are becoming less and less necessary to program in an assembly
language. A high level language made programs portable and programming
more productive. There are however situations where an assembly language
is necessary. When the situation demands programming at very near
hardware level. Compiler writer, device driver in OS, embedded controller
etc. need to use assembly language. Assembly language is the language
that allows a programmer to talk about operation on a bare bone hardware.
For a computer architect, an assembly language is the "interface" of the
hardware design. During this course, we will talk about the innard
of computers, their organization, how each units work. All these follow
from what kind of assembly language a computer has. It is necessary
for a computer architect to be able to write and read assembly language
well. All working units inside a computer perform according to some
sequence of its instruction.
To study computer architecture, we need to be able to understand assembly
language. This introduction will concentrate on principles of assembly
language programming. The aim is to enable students to read some
subset of assembly language and understand their operational semantics.
We will use a hypothetical processor designed and developed recently for
teaching purpose S2. It has simple instruction
set and is easy to understand.
Assembly language programming
S2 instruction set: S2 has 32 registers, each register holds a 32-bit number.
-
accessing variables: ld st
-
arithmetic: add sub mul div
-
logic: and or xor shr shl
-
control flow: jmp conditional, jump, jal, jr
-
special: trap
-
format of instruction: operation destination source
The use of registers is for performance reason. It is faster to access
registers than to access memory. A register is used to
hold an intermediate result.
Example
Example A = B + C - D
Let r1 = A, r2 = B, r3 = C, r4 = D
ld r2 B
ld r3 C
ld r4 D
add r2 r2 r3
sub r1 r2 r4
st A r1
Example sum from 1 to 10
s = 0, i = 1,
while(i<=10){
s = s + i, i= i + 1
}
Let r1 = S, r2 = i
.s
S 100
I 101
.a 0
.c
st S r0
;; s = 0
ld r1 #1
st I r1
;; i = 1
:loop ld r2 I
sub r0 r2 #10
jmp GT exit ;;
while i <= 10
ld r1 S
ld r2 I
add r1 r1 r2
st S r1
;; s = s + i
add r2 r2 #1
st I r2
;; i = i + 1
jmp always loop
:exit trap stop r0
.e
Tools
An assembler can translate a source file into machine code file (in some
file format). This machine code file can be loaded into a simulator
and executed. A simulator allows students to execute and monitor
the effect step by step. It shows the value of all registers and
can display memory values.
Homework
1) write an assembly program to do P = 2 x M - N
2) write an assembly program to find sum AR[i] i = 1..5
Run it under the simulator, check the result.
last update 10 June 2003