Chapter 6: Machine Language and Assembler Christian Jacob
1 Classical Universal Computer 3
1.1 “Von Neumann Architecture” 3
1.2 CPU and RAM 5
1.3 Arithmetic Logical Unit (ALU) 6
1.4 Arithmetic Logical Unit (ALU) 7
2 Machine Language 10
2.1 Hypothetical Machine 12
2.2 Machine Language 13
2.3 Execution of Machine Programs 15
3 Assembler Language 16
3.1 Structure of Assembler Commands 17
3.2 From Assembler to Machine Code 19
3.3 Operand Addressing 20
3.4 Stack 27
3.5Symbolic Names 29
Chapter 6: Machine Language and Assembler Christian Jacob
4 Programming in Assembler 31
4.1 Alternatives 31
4.2 Loops 32
4.3 Sub-routines 34
4.4 Macros 39
4.5Registers 41
Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob
1 Classical Universal Computer
1.1 “Von Neumann Architecture”
Burks, Goldstine, von Neumann (Princeton, 1946/47)
Control Unit
Memory
Input Unit Output Unit
ALU
Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob
“Von Neumann Architecture”
• The computer architecture is problem-independent.
--> Universal Computer:
- Arithmetic Logical Unit - Memory
- Control Unit
- Input / Output Unit
• Program and data both reside in memory.
• Each memory location has an address, through which its contents can be accessed.
• In general, program commands are stored in consecutive memory locations.
• There are jump commands.
Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob
1.2 CPU and RAM
RAM CPU
Random Access Memory
Central Processing
Unit
RAM contains data
and instructions (programs) how to process the data
The CPU processes data Data bus transports
data from RAM to the CPU for processing
Data bus transports the processed data to RAM, so it can be displayed,
output, or stored on disk
Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob
1.3 Arithmetic Logical Unit (ALU)
RAM
Control Unit
Register 1 Register 2
Data Result
ALU
op
Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob
1.4 Arithmetic Logical Unit (ALU)
RAM
Control Unit
Register 1 Register 2
Accumulator
Data Result
ALU
+
4 5
9
Addition
Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob
ALU / RAM Performance:
• 1. The data to be processed arrives from RAM and is held in registers.
• 2. A signal from the Control Unit indicates which arithmetic or logical operation to perform.
• 3. The ALU performs the operation and places the result in the accumulator.
• 4. The results are usually sent to RAM so that they can be output or stored on disk.
Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob
A more detailed look at a von Neumann architecture machine
OP: operator part OC: operation control LC: logical circuits AP: address part MR: memory register ACC: accumulator
IP: instruction pointer MSR: memory selection reg. QR: quotient register OD: operator decoding MR: multiplier register IOR: input/output reg.
OP AP
IP
OD
OC
IOR MR
Memory
MR
ACC QR
LC
RAM
Feedback / Control Signals
Command Unit
Control Unit Memory Unit Arithmetic Logical Unit I/O Unit
ALU
MSR
Machine Language Chapter 6: Machine Language and Assembler Christian Jacob
2 Machine Language
• Elementary operations:
- data transfer - program control
- arithmetic and logic operations - move operations
- interrupt handling
• Binary command representation
• regular structure of all the commands
Machine Language Chapter 6: Machine Language and Assembler Christian Jacob
• command formats:
- 1-address command
- 2-address command
- 3-address command
Operation Address of Operand 2
Operation Address of
Operand 1 & Result
Address of Operand 2
Operation Address of
Operand 2
Address of Result Address of
Operand 1
Machine Language Chapter 6: Machine Language and Assembler Christian Jacob
2.1 Hypothetical Machine
• Word length: 16 bit
• Structure of a memory cell (1-address command)
Op Code: command code for an operation D: label for a double word
Mod: modification part (using index registers) Address/value: contains address or value of the operand
Op Code D Mod Address / Value
1512 11 10 8 7 0
Machine Language Chapter 6: Machine Language and Assembler Christian Jacob
2.2 Machine Language
Opcode dual hex. Description of the Operation Mnemo.
0000 0 Halt. End of program execution HLT
0001 1 Load operand to accumulator LOA
0010 2 Store accumulator at designated address STI
0011 3 Add operand to accumulator ADD
0100 4 Subtract operand from accumulator SUB
0101 5 Multiply operand with accumulator MUL
0110 6 Divide accumulator by operand DIV
0111 7 Unconditional jump JMP
1000 8 Jump if accumulator = 0 JEZ
1001 9 Jump if accumulator > 0 JGZ
1010 A Jump if accumulator < 0 JLZ
1100 C Stack commands -
1101 D Jump to subroutine JSR
1110 E Return from subroutine RET
1111 F Index commands -
Machine Language Chapter 6: Machine Language and Assembler Christian Jacob
Example: The word 0011 0100 1001 1101 in a memory cell can be …
• decimal number: 13469
• command:
- 0011: add operand to accumulator
- 0: one word command
- 100: constant operand (“immediate operand”) - 10011101: 15710
--> “Add 157 to the contents of the accumulator.”
0011 0 100 10011101
Machine Language Chapter 6: Machine Language and Assembler Christian Jacob
2.3 Execution of Machine Programs
Instruction Pointer
Memory Addressing
RAM
Control Unit
ALU Accu
Operator Address / Constant Instruction Register
Program address
Increment instruction pointer
Load instruction
Load/store data Load constant
Fetch opcode and mod Load data address
Load jump address
1
2
3
4
3
5
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
3 Assembler Language
• Disadvantages of machine language:
- Binary / hexadecimal representation of operations and operands - Use of fixed addresses
- No commentary
• Assembler
- Mnemo-technical names of operations - Various number systems
- Commentaries - Macros
• Example
machine language: 0011 0100 1001 1101
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
3.1 Structure of Assembler Commands
• Label
- characterizes an assembler command
- jump commands can use this label as destination - usually a string consisting of 4 to 8 letters
- is not inserted into the (generated) machine code
• Operation
- mnemo-technical representation of the Op-code
• Operand(s)
- contains the operands of the assembler command (value of the operand or an address)
- (constant) expressions may be allowed
• Register field
- for machines with 2-address commands: reference to a register which contains one of the operands
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
• Index field
- refers to the register, the contents of which is to be added to the operand address.
• Commentary
- remarks and comments on the program text - not inserted into the (generated) machine code
Formating of assembler code
• fixed structure, column-oriented or
• fields separated by special characters (example: labels by “:”, comments by
“;”) or
• field separators (tabs, space, …)
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
3.2 From Assembler to Machine Code
label operator operand register field commentary index register
operator mod operand(s)
Assembler:
Machine code:
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
3.3 Operand Addressing
Effective address = address generated by interpreting (or by address calculation) or the modification field
• Immediate operand commands
- the address field contains the operand as a constant - labelled by #
- machine code: Mod = 100
- 8 bit (24 bit for 2-word commands)
--> integer numbers between -128 and 127
- Example:
ADD, #26 ; add 26 to accumulator
Accumulator Accumulator
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
• Direct address
- effective address = contents of address field - labelled by @
- machine code: Mod = 0
- bits 8 and 9 (Mod) are used as additional address bits --> 210 = 1024 memory cells are directly addressable
- Example:
STI, @649
; store accu at address 649123 Accumulator
RAM
649 123
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
• Relative addressing
- effective address = command address + address field
--> the program is relocatable in memory without any recalculation of the addresses
- no labelling
- machine code: Mod = 101
- Example:
ADD, 148
; add value at relative address 148x+123 Accumulator
RAM
248 123
100
ADD, 148
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
• Indirect addressing
- effective address = value at the address given in the address field - labelled by (...)
- machine code: Mod = 110
- Example:
STI, (148)
; indirect addressing123 Accumulator
RAM
250 123
148 250
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
• Index register addressing
- effective address = address field + contents of index register
--> Without any program changes, all effective addresses can be changed by modifying the contents of the index register.
- labelled by %
- machine code: Mod = 111
- Example:
LOA, %128
; use index register123 Accumulator
RAM
228 123 100
Index Register
+ 128
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
- special index register commands:
Opcode Mod Ass. Description
1111 000 CLI Load 0 to index register.
1111 001 LDI Load accu into index register.
1111 010 LIA Load index register into accu.
1111 011 INC Increment index register by 1.
1111 100 DEC Decrement index register by 1.
1111 111 LDX Load operand into index register.
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
• Example: Programming with an index register
LDX, @1030 ;
load index register with contents;
of memory cell 1030LOA, #15 ; load 15 to accumulator
ADD, %3 ;
add the contents of memory cell; 3+index register to the accu
STI, (1031) ;
store the result at the address; contained in memory cell 1031
HLT ;
end of programStep Index reg. Accu Memory
1 1050 - 1030: 1050
1031: 1039
… 1053: 17
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
3.4 Stack
• LIFO principle (Last In, First Out)
• For binary operations:
- first operand is the second element from the top of stack - second operand is the top of stack element
- the operation result replaces the two top-most stack elements A
B Push A Push B
A Push C
B A C Pop
B A Pop
A Pop
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
• Stack Commands:
Opcode Mod Ass. Description
1100 0001 PUSHAC Load accumulator to stack
1100 0010 POPAC Load top stack element to accumula- tor and remove from stack
1100 0011 ADDST Addition 1100 0100 SUBST Subtraction 1100 0101 MULST Multiplication 1100 0110 DIVST Division
1100 1011 PUSH Load operand to stack
1100 1100 POP Store top stack element at desig-
nated address and remove from stack
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
3.5 Symbolic Names
Symbolic names can be associated with an address:
• Value
The name is associated to a numerical value.
Example:
NUMB EQUIVALENT 1073 ...
LOA, @NUMB
• Labels
Labels are symbolic names associated to an assembler command.
Example:
START: LOA, #5
…
JMP, @START
Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob
• Data definitions and memory allocation
Example:
LOA, @CONST
MUL, #2
STI, @ARRAY HLT
CONST: DATA, #300 ; memory cell initialized
; with a value of 300
ARRAY: BLOCK,20 ; array of 20 memory cells
Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob
4 Programming in Assembler
4.1 Alternatives
if ( <condition> ) <command A> else <command B>
Example: if ( x < y ) A else B Assembler:
LOA, @x ; accu = x
SUB, @y ; accu = x-y JLZ, @LABELA ; x-y < 0 ?
<B>
LABELA: <A>
CONTINUE:
Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob
4.2 Loops
Example:
C++:
do {
x = 4*a;
a = a+k;
} while ( a <= endvalue );
Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob
Assembler:
LOOP: LOA, #4 ; accu = 4
MUL, @a ; accu = 4*a STI, @x ; x = 4*a
LOA, @a ; accu = a ADD, @k ; accu = a+k STI, @a ; a = a+k
SUB, @endvalue; accu = a-endvalue JLZ, @LOOP ; a < endvalue ?
JEZ, @LOOP ; a = endvalue ?
do {
x = 4*a;
a = a+k;
} while ( a <= endvalue );
Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob
4.3 Sub-routines
main program
...
call of subroutine
end of program
...
sub routine
...
Two Problems:
• Where to store the return address?
JSR
Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob
Return Addresses:
- store in memory cell before or after the subroutine code - use a stack
Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob
Using a stack for storing return addresses
sub routine A
...
call of B
...
return main program
...
call of A
...
sub routine B
...
1000
1310
1385
1415
1470
Command Instruction
Pointer Stack
Start main program 1000 -
Call subroutine A 1310 -
Jump to subroutine A 13851311
Call subroutine B 14151311
Jump to subroutine B 1470 1416 1311 Return from subroutine B 1550 1416 1311 Continue in subroutine A 1416 1311 Return from subroutine B 1430 1311
1430
Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob
Passing of Parameters and Return of Subroutine Results
• Parameter passing using the accumulator or another register --> number of parameters is limited
• Store parameters at specific memory addresses and reading in from within the subroutine
--> complex memory management, hard to keep up with used memory space
• Use memory section prior to the subroutine
--> enables relative addressing of the parameters within the subroutine
• Use memory section after the subroutine --> enables relative addressing, too
• Parameter passing using a stack
Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob
Parameter Passing Using a Stack
; Main program
…
PUSH, @PARAM1 PUSH, @PARAM2
PUSH, #7 ; return address JSR, @UP1
POP, @RESULT1
…
UP1: POP, @RETURNADDR ; subroutine POP, @UPPARAM2
POP, @UPPARAM1
… ; subroutine calc.
PUSH, @UPRESULT
PUSH, @RETURNADDR
Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob
4.4 Macros
• Macros perform textual substitutions
• Macros are expanded before the assembler is started
Example:
MACRO, sum ; macro head LOA, @OP1 ; macro body ADD, @OP2
STI, @RESULT MACROEND
Program Text Macro Generator
Assembler
Commands Assembler
Program in Machine
Code
Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob
Application of the macro sum:
SPI, @OP1 LOA, #1024 STI, @OP2 sum
LOA, @RESULT
SPI,@OP1
LOA, #1024 STI, @OP2 LOA, @OP1 ADD, @OP2
SPI, @RESULT
LOA, @RESULT
Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob
4.5 Registers
• Today´s computers have many more registers
--> The assembler must be able to address these registers.
• Extension of assembler language:
Example: