Chap 03 Introduction To Assembly Language Programming
Chap 03 Introduction To Assembly Language Programming
ASSEMBLY LANGUAGE
PROGRAMMING
Assembly Language
MARIE ARCHITECTURE
Assembly Language
MARIE ARCHITECTURE
0000
• Assume the number of address bits
0001
is 4 bits.
0010
0011
For this example, the maximum 0100
number of memory locations is 16. 0101
0110
• The maximum number of 0111 16 memory
addressable memory locations (the 1000 locations
size of the main memory) is equal 1001
to 2n where n is the number of 1010
address bits. 1011
1100
• Since MARIE uses 12-bit addresses, 1101
it has 212 = 4,096 addressable main 1110
memory locations, (from memory 1111
address 0 to 4,095). Main Memory
Assembly Language
MARIE ARCHITECTURE
Assembly Language
MARIE ARCHITECTURE
Assembly Language
MARIE ARCHITECTURE
Memory Address 0
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• This is the format of a MARIE instruction:
• The most significant 4 bits (bit 12 • The least significant 12 bits (bit 0
to bit 15) make up the Opcode to bit 11) form a memory address
that specifies the instruction to (usually for an operand).
be executed .
This allows for a maximum
This allows for a total of 24 = 16 memory size of 212 = 4,096
instructions. memory locations.
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• This is the format of a MARIE instruction:
Example:
0001001110011111 (or 139F16)
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Most ISAs consist of instructions for processing data, moving data, and controlling
the execution sequence of the program.
Instruction
Number Instruction Meaning
Bin Hex
0001 1 Load X Load the contents of Address X into AC
0010 2 Store X Store the contents of AC to Address X
0011 3 Add X Add the contents of Address X to AC and store results in AC
0100 4 Subt X Subtract the contents of Address X from AC and store results in AC
0101 5 Input Input the value of the keyboard into AC
0110 6 Output Output the value in AC to the display
0111 7 Halt Terminate the program
1000 8 Skipcond Skip the next instruction on condition
1001 9 Jump X Load the value of X into PC
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Load X (Load the contents of Address X into AC)
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Load X (Load the contents of Address X into AC)
Instruction
Number Instruction All data from memory must be moved first into
Bin Hex
the MBR and then into either the AC or the ALU;
there are no other options in this architecture.
0001 1 Load X
0010 2 Store X
0011 3 Add X Notice that the Load X instruction does not have
0100 4 Subt X to name the AC as the final destination; this
0101 5 Input register is implicit in the instruction.
0110 6 Output
0111 7 Halt
Other instructions reference the AC register in a
1000 8 Skipcond
similar fashion.
1001 9 Jump X
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Load X (Load the contents of Address X into AC)
Example:
Instruction
Number Instruction (or139F
0001001110011111 (or 139F1616) )
Bin Hex
0001 1 Load X The leftmost 4 bits indicate the opcode or the
0010 2 Store X
instruction to be executed. 00012 (or 116)
represents the Load X instruction.
0011 3 Add X
0100 4 Subt X
The remaining 12 bits (0011100111112 or 39F16)
0101 5 Input indicate the 12-bit main memory address of the
0110 6 Output value which is to be loaded to AC.
0111 7 Halt
1000 8 Skipcond So the instruction in assembly is Load 39F.
1001 9 Jump X
This instruction will therefore cause the data
value found in main memory address 39F16 to be
loaded or copied into the AC.
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Store X (Store the contents of AC to Address X)
The Store X instruction allows the movement of
data from the CPU back to memory.
Instruction
Number Instruction Example:
Bin Hex
0010010001110111 (or
(or139F
2477 ))
1616
0001 1 Load X
0010 2 Store X The leftmost 4 bits are 00102 (or 216) which is the
0011 3 Add X Store X instruction.
0100 4 Subt X
0101 5 Input The remaining 12 bits (0100011101112 or 47716)
0110 6 Output
indicate the memory address where the contents
of AC will be copied to.
0111 7 Halt
1000 8 Skipcond
So the instruction in assembly is Store 477.
1001 9 Jump X
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Add X (Add the contents of Address X to AC and
store results in AC)
Instruction
Number Instruction Example:
Bin Hex
0001 1 Load X 0011100001011101 (or 385D
139F16
16))
0010 2 Store X
0011 3 Add X
The leftmost 4 bits are 00112 (or 316) which is the
Add X instruction.
0100 4 Subt X
0101 5 Input
The remaining 12 bits (1000010111012 or 85D16)
0110 6 Output indicate the address of the operand to be added
0111 7 Halt to AC.
1000 8 Skipcond
1001 9 Jump X So the instruction in assembly is Add 85D.
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Add X (Add the contents of Address X to AC and
store results in AC)
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Subt X (Subtract the contents of Address X from
AC and store results in AC)
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Subt X (Subtract the contents of Address X from
AC and store results in AC)
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Input (Input the value of the keyboard into AC)
The Input instruction allows MARIE to get data
Instruction from the outside world via the keyboard. The data
Number Instruction
from the keyboard is moved first into InREG and
then into the AC.
Bin Hex
0001 1 Load X
This instruction does not need an address since
0010 2 Store X the data will come from the keyboard and it will
0011 3 Add X be stored in AC. So the address field of the
0100 4 Subt X
instruction will be all 0's (twelve 0's).
0101 5 Input
Example:
0110 6 Output
(or439F
0101000000000000 (or 5000 ))
1616
0111 7 Halt
1000 8 Skipcond
The leftmost 4 bits are 01012 (or 516) which is the
1001 9 Jump X Input instruction.
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Skipcond (Skip the next instruction on condition)
Instruction
Number Instruction
Bin Hex
0001 1 Load X
0010 2 Store X if condition is if condition is
false true
0011 3 Add X skipcond ___
0100 4 Subt X
0101 5 Input
0110 6 Output
0111 7 Halt
"Skip" means
1000 8 Skipcond
jump over
1001 9 Jump X the next
instruction.
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Skipcond (Skip the next instruction on condition)
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Skipcond (Skip the next instruction on condition)
Instruction
Number Instruction
Bin Hex
0001 1 Load X
0010 2 Store X
0011 3 Add X bit 11 bit 10 Meaning
0100 4 Subt X 0 0 Skip if AC is less than 0
0101 5 Input
0 1 Skip if AC is equal to 0
0110 6 Output
1 0 Skip if AC is greater than 0
0111 7 Halt
1000 8 Skipcond
1001 9 Jump X Since "skip" means jump over the next
instruction, this is accomplished by incrementing
the PC by 1, essentially ignoring the following
instruction, which is never fetched.
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Skipcond (Skip the next instruction on condition)
Example:
Instruction
Number Instruction
Bin Hex 880016
1000100000000000 (or 469F 16))
0001 1 Load X
0010 2 Store X
0011 3 Add X The leftmost 4 bits are 1000 (or 816) which is the
0100 4 Subt X Skipcond instruction.
0101 5 Input
0110 6 Output
Bit 11 and bit 10 of the address field are 10. This
0111 7 Halt
implies a "skip if AC is greater than 0."
1000 8 Skipcond
1001 9 Jump X
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Skipcond (Skip the next instruction on condition)
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Skipcond (Skip the next instruction on condition)
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Jump X (Load the value of X into PC)
0100 4 Subt X
.
0101 5 Input .
0110 6 Output .
0111 7 Halt
200 LOAD 477
1000 8 Skipcond
201 ADD 135
1001 9 Jump X
Main Memory
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Jump X (Load the value of X into PC)
The Jump X instruction, an unconditional branch,
Instruction also affects the PC. This instruction causes the
Number contents of the PC to be replaced with the value
Instruction of X, which is the address of the next instruction
Bin Hex to fetch (the target of the jump).
0001 1 Load X
0010 2 Store X Example:
0011 3 Add X 1001101000110101 (or
(or469F
9A35 ))
1616
0100 4 Subt X
0101 5 Input The leftmost 4 bits are 10012 (or 916) which is the
0110 6 Output
Jump instruction.
0111 7 Halt
The remaining 12 bits (101000110101 or A3516)
1000 8 Skipcond
indicate the address of the next instruction to be
1001 9 Jump X fetched.
Assembly Language
REGISTER TRANSFER LANGUAGE
Assembly Language
REGISTER TRANSFER LANGUAGE
• Load X
0 0 0 1 X
15 12 11 0
MAR X
MBR M[MAR]
AC MBR
Assembly Language
REGISTER TRANSFER LANGUAGE
Assembly Language
REGISTER TRANSFER LANGUAGE
• Store X
0 0 1 0 X
15 12 11 0
MAR X
MBR AC
M[MAR] MBR
Assembly Language
REGISTER TRANSFER LANGUAGE
• Add X
0 0 1 1 X
15 12 11 0
The data value stored at address X is added to the AC and the result
is stored back to the AC.
MAR X
MBR M[MAR]
AC AC + MBR
Assembly Language
REGISTER TRANSFER LANGUAGE
• Subt X
0 1 0 0 X
15 12 11 0
MAR X
MBR M[MAR]
AC AC – MBR
Assembly Language
REGISTER TRANSFER LANGUAGE
• Input
0 1 0 1 0 0 0...0
15 12 11 0
Any input from the input device is first routed into the InREG. Then
the data is transferred into the AC.
AC InREG
Assembly Language
REGISTER TRANSFER LANGUAGE
• Output
0 1 1 0 0 0 0...0
15 12 11 0
OutREG AC
Assembly Language
REGISTER TRANSFER LANGUAGE
• Halt
0 1 1 1 0 0 0...0
15 12 11 0
Assembly Language
REGISTER TRANSFER LANGUAGE
• Skipcond
1 0 0 0 000...0
15 12 11 10 9 0
Assembly Language
REGISTER TRANSFER LANGUAGE
• Skipcond
1 0 0 0 000...0
15 12 11 10 9 0
if IR[11–10] = 00 then
if AC < 0 then PC PC + 1
else if IR[11–10] = 01 then
if AC = 0 then PC PC + 1
else If IR[11–10] = 10 then
if AC > 0 then PC PC + 1
Assembly Language
REGISTER TRANSFER LANGUAGE
• Jump X
1 0 0 1 X
15 12 11 0
PC X
Assembly Language
DISASSEMBLING MARIE INSTRUCTIONS
X = 000000001101
= 00D16
Add the contents of memory location 00D16 to the contents
of the AC and store the results back to the AC.
Assembly Language
DISASSEMBLING MARIE INSTRUCTIONS
21E216 = 0010000111100010
X = 000111100010
= 1E216
Store the contents of the AC to memory location 1E216.
Assembly Language
INSTRUCTION PROCESSING
Assembly Language
INSTRUCTION PROCESSING
• The steps in this cycle, which take place in specific clock cycles, are
listed below:
Take note that Steps 1 and 2 make up the fetch phase of the cycle.
Assembly Language
INSTRUCTION PROCESSING
Decode IR[15–12]
MAR IR[11–0] send the address field to MAR to
prepare for any operand fetch
IR
Assembly Language
INSTRUCTION PROCESSING
MBR M[MAR]
Execute the actual instruction
Assembly Language
INSTRUCTION PROCESSING
• In summary:
Step RTL
Fetch MAR PC
IR M[MAR]
PC PC + 1
Decode Decode IR[15–12]
MAR IR[11–0]
Get Operand MBR M[MAR] (if necessary)
Execute
Assembly Language
INSTRUCTION PROCESSING
Assembly Language
INSTRUCTION PROCESSING
• Consider the simple MARIE program given below showing a set of
mnemonic instructions stored at addresses 100 - 106 (hex):
Address Instruction Binary Contents Hex Contents
100 Load 104 0001000100000100 1104
101 Add 105 0011000100000101 3105
102 Store 106 0010000100000110 2106
103 Halt 0111000000000000 7000
104 0023 0000000000100011 0023
105 FFE9 1111111111101001 FFE9
106 0000 0000000000000000 0000
Assembly Language
INSTRUCTION PROCESSING
• Load 104
Address Instruction
100 Load 104 Load the contents of memory
101 Add 105
location 104 to AC.
102 Store 106
103 Halt
104 0023
105 FFE9
106 0000 AC 0023
Assembly Language
INSTRUCTION PROCESSING
• Add 105
Address Instruction
100 Load 104
Add the contents of AC to the
contents of memory location
101 Add 105
105 and store the results to
102 Store 106 AC.
103 Halt
104 0023
FFE9 + 0023 = 000C
105 FFE9
106 0000
AC 000C
Assembly Language
INSTRUCTION PROCESSING
• Store 106
Address Instruction
100 Load 104 Store the contents of AC to
101 Add 105
memory location 106.
102 Store 106
103 Halt
104 0023
105 FFE9
106 0000 106 000C
Assembly Language
INSTRUCTION PROCESSING
• The fetch-decode-execute
cycle starts by fetching the
first instruction of the
program, which it finds by
loading the PC with the • The list of instructions under the Binary
address of the first instruction Contents of Memory Address column
when the program is loaded constitutes the actual machine language
for execution. program.
Assembly Language
INSTRUCTION PROCESSING
Assembly Language
INSTRUCTION PROCESSING
• The Add 105 instruction adds the
contents of memory location 105 to
the AC and store the results into AC.
Assembly Language
INSTRUCTION PROCESSING
Assembly Language
INSTRUCTION PROCESSING
Assembly Language
ASSEMBLERS
Assembly Language
ASSEMBLERS
Assembly Language
ASSEMBLERS
Assembly Language
ASSEMBLERS
Assembly Language
ASSEMBLERS
Partial 1
Translation 3 X 104
Symbol
of 2 Table Y 105
Instructions 7 000 Z 106
Assembly Language
ASSEMBLERS
Complete 1 104
Translation 3 105 X 104
Symbol
of 2 106 Table Y 105
Instructions 7 000 Z 106
Assembly Language
ASSEMBLERS
Assembly Language
ASSEMBLERS
Assembly Language
ASSEMBLERS
Assembly Language
EXERCISE
• Consider the following MARIE assembly language program:
Assembly Language
MARIE PROGRAMMING
if X = Y then
X= X*2
else
Y=Y–X
Assembly Language
MARIE PROGRAMMING
if X = Y then
X= X*2
else Load X AC = X Else, Load Y AC = Y
Y=Y–X Subt Y AC = AC - Y Subt X AC = AC - X
Skipcond 400 If AC = 0, skip Store Y Y = AC
AC = X – Y
Jump Else If AC 0 Halt
AC < 0, X < Y Load X AC = X
AC = 0, X = Y
AC > 0, X > Y
Add X AC = AC + X
Store X X = AC
Halt X, DEC 12
Y, DEC 10
Assembly Language
MARIE PROGRAMMING
Assembly Language
MARIE PROGRAMMING
Assembly Language
MARIE PROGRAMMING
Assembly Language
MARIE PROGRAMMING
if (i < = 10)
sum = 0;
else
sum = 1;
Assembly Language
MARIE PROGRAMMING
if (i < = 10)
Load I AC = I Then, Load Zero AC = Zero
sum = 0;
else Subt Ten AC = AC - Ten Store Sum Sum = AC
sum = 1; Skipcond 800 If AC > 0, skip End, Halt
Jump Then If AC <= 0
Load One AC = One
Store Sum Sum = AC
Jump End
Sum, DEC 0
AC = I – Ten I, DEC 0
AC < 0, I < Ten
Zero, DEC 0
AC = 0, I = Ten One, DEC 1
AC > 0, I > Ten Ten, DEC 10
Assembly Language
MARIE PROGRAMMING
Assembly Language
MARIE PROGRAMMING
Assembly Language
MARIE PROGRAMMING
x = 1;
while (x < 10)
x = x + 1;
Assembly Language
MARIE PROGRAMMING
AC = X – Ten
Store X X = AC X, DEC 0
Jump Loop One, DEC 1
AC < 0, X < Ten End, Halt Ten, DEC 10
AC = 0, X = Ten
AC > 0, X > Ten
Assembly Language
MARIE PROGRAMMING
Assembly Language
EXERCISES
sum = 0;
for (i = 0; i <= n; i++)
sum = sum + i;
Assembly Language
EXERCISES
Assembly Language