You are on page 1of 87

INTRODUCTION TO

ASSEMBLY LANGUAGE
PROGRAMMING

SCHOOL OF COMPUTING MITCH M. ANDAYA


INTRODUCTION TO MARIE

• MARIE, a Machine Architecture that is Really Intuitive and Easy, is


a simple architecture consisting of memory (to store programs and
data) and a CPU (consisting of an ALU and several registers).

• It has all the functional components necessary to be a real working


computer.

• It was designed for the singular purpose of illustrating basic


computer system concepts.

• While this system is too simple to do anything useful in the real


world, a deep understanding of its functions will facilitate the
comprehension of system architectures that are much more
complex.

Assembly Language
MARIE ARCHITECTURE

• The MARIE architecture has the following characteristics:

‒ It uses a two's complement binary system for data


representation.

‒ It is stored program with fixed word length data and


instructions where all instructions and data are 16 bits long.

‒ It has a 16-bit arithmetic logic unit (ALU).

‒ It has seven registers for control and data movement.

‒ It uses 12-bit memory addresses.

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

• MARIE's seven registers are:

‒ Accumulator, AC, a 16-bit register that holds one


operand of a two-operand instruction. AC is the only
general-purpose register of MARIE.

‒ Memory Address Register, MAR, a 12-bit register that


holds the memory address of an instruction or the
operand of an instruction.

‒ Memory Buffer Register, MBR, a 16-bit register that


holds the data after its retrieval from, or before its
placement in memory.

Assembly Language
MARIE ARCHITECTURE

‒ Program Counter, PC, a 12-bit register that holds the


address of the next program instruction to be executed.

‒ Instruction Register, IR, which holds an instruction


immediately preceding its execution.

‒ Input Register, InREG, an 8-bit register that holds data


read from an input device.

‒ Output Register, OutREG, an 8-bit register, that holds


data that is ready for the output device.

Assembly Language
MARIE ARCHITECTURE
Memory Address 0

Memory Address 4,095

Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE

• The instruction set architecture (ISA) of a machine


specifies the instructions that the computer can
perform and the format for each instruction.

• The ISA is an interface between a computer's


hardware and its software.

• Some ISAs include hundreds of different


instructions for processing data and controlling
program execution.

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)

0001 or 116 is the 0001001110011111 001110011111 or 39F16 is


opcode of the the 12-bit address field of
instruction. the instruction.

It represents the Load It represents the address of


instruction. the data to be loaded.

Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Most ISAs consist of instructions for processing data, moving data, and controlling
the execution sequence of the program.

• MARIE's instruction set consists of the instructions:

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)

The Load X instruction allows the movement of


Instruction data from memory into the CPU.
Number Instruction
Bin Hex
0001 1 Load X
0010 2 Store X
AC
0011 3 Add X
0100 4 Subt X X
0101 5 Input
0110 6 Output
0111 7 Halt
1000 8 Skipcond
1001 9 Jump X CPU Main Memory

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

This instruction will therefore cause the contents


of AC to be stored or copied to main memory
address 47716.
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Add X (Add the contents of Address X to AC and
store results in AC)

Instruction The Add X instruction adds the data value found


Number Instruction at address X to the value in the AC and the results
Bin Hex are stored back to AC.
0001 1 Load X
0010 2 Store X
0011 3 Add X AC MBR
0100 4 Subt X
0101 5 Input X
0110 6 Output +
0111 7 Halt
1000 8 Skipcond
1001 9 Jump X
CPU Main Memory

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)

Instruction If AC contains 10EF16 and memory location 85D16


Number Instruction contains 3D0216, then the following addition will
Bin Hex take place:
0001 1 Load X
0010 2 Store X
0011 3 Add X ‭ 0001000011101111‭ (or 10EF16)
0100 4 Subt X + ‭0011110100000010‭ (or 3D0216)
0101 5 Input
0110 6 Output
‭0100110111110001‭ (or 4DF116)
0111 7 Halt
1000 8 Skipcond
The result of the addition (4DF116) will be stored
1001 9 Jump X back to AC (overwriting the previous contents
which was 10EF16).

Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Subt X (Subtract the contents of Address X from
AC and store results in AC)

Instruction The Subt X instruction subtracts data value found


Number Instruction at address X from the value in the AC and the
Bin Hex
results are stored back to AC.
0001 1 Load X
Example:
0010 2 Store X
0011 3 Add X
(or439F
0100100001011101 (or 485D ))
1616
0100 4 Subt X
0101 5 Input
The leftmost 4 bits are 01002 (or 416) which is the
0110 6 Output Subt X instruction.
0111 7 Halt
1000 8 Skipcond The remaining 12 bits (1000010111012 or 85D16)
1001 9 Jump X indicate the address of the operand to be
subtracted from AC.

So the instruction in assembly is Subt 85D.

Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Subt X (Subtract the contents of Address X from
AC and store results in AC)

Instruction If AC contains 10EF16 and memory location 85D16


Number Instruction contains 3D0216, then the following subtraction
Bin Hex will take place:
0001 1 Load X
2's complement
0010 2 Store X of 3D0216
0011 3 Add X ‭ 0001000011101111‭(or 10EF16)
0100 4 Subt X ‭
+‭‭10011110100000010‭
100001011111110‭ (or 3D0216)
0101 5 Input
0110 6 Output
‭ 101001111101101‭‭ (or D3ED16)
1
0111 7 Halt
1000 8 Skipcond
The result of the addition (D3ED16) will be stored
1001 9 Jump X back to AC (overwriting the previous contents
which was 10EF16).

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.

The remaining 12 bits are all 0's. So the


instruction in assembly is simply Input.
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Output (Output the value in AC to the display)
The Output instruction allows MARIE to send data
Instruction
to the outside world via the display monitor. The
Number
data from AC is moved first into OutREG and then
Instruction sent to the monitor.
Bin Hex
0001 1 Load X This instruction does not need an address since
0010 2 Store X the data will come from AC and is sent to the
0011 3 Add X monitor. So the address field will be all 0's
(twelve 0's).
0100 4 Subt X
0101 5 Input
Example:
0110 6 Output
(or469F
0110000000000000 (or 6000 ))
1616
0111 7 Halt
1000 8 Skipcond The leftmost 4 bits are 01102 (or 616) which is the
1001 9 Jump X Output instruction.

The remaining 12 bits are all 0's. So the


instruction in assembly is simply Output.
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Halt (Terminate the program)

The Halt command causes the current program


Instruction execution to terminate.
Number Instruction
Bin Hex This instruction does not need an address since
0001 1 Load X
no data is processed. So the address field will be
all 0's (twelve 0's).
0010 2 Store X
0011 3 Add X
Example:
0100 4 Subt X
0101 5 Input (or469F
0111000000000000 (or 70001616) )
0110 6 Output
0111 7 Halt The leftmost 4 bits are 01112 (or 716) which is the
1000 8 Skipcond Halt instruction.
1001 9 Jump X
The remaining 12 bits are all 0's.

So the instruction is written as Halt.


Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Skipcond (Skip the next instruction on condition)

Instruction The Skipcond instruction allows conditional


Number Instruction branching (as is done with "while" loops or "if"
Bin Hex
statements).
0001 1 Load X
When the Skipcond instruction is encountered,
0010 2 Store X
the CPU checks if a certain condition is true or
0011 3 Add X not.
0100 4 Subt X
0101 5 Input If the condition is true, the next instruction is
0110 6 Output skipped.
0111 7 Halt
1000 8 Skipcond
If the condition is false, then program execution
proceeds to the next instruction (as if nothing
1001 9 Jump X happened).

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)

Instruction When the Skipcond instruction is executed, the


Number Instruction value stored in the AC must be inspected to
Bin Hex
determine if a "skip" will be performed or not.
0001 1 Load X
The two most significant bits of the address field
0010 2 Store X
of the instruction (bit 10 and bit 11) specify the
0011 3 Add X condition to be tested.
0100 4 Subt X
0101 5 Input
0110 6 Output 1 0 0 0 X X
0111 7 Halt 15 12 11 10 9 1 0

1000 8 Skipcond op-code address


1001 9 Jump X field field

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

The assembly instruction is Skipcond 800.

Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Skipcond (Skip the next instruction on condition)

If the value in the AC is greater than 0, the


condition is true.

So PC will be incremented by 1, thus causing the


instruction immediately following this instruction
in the program to be ignored or skipped.

If the value in the AC is not greater than 0 (AC ≤


0), the condition is false.

So this instruction is ignored and the program


simply goes on to the next instruction.

Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Skipcond (Skip the next instruction on condition)

Instruction Take note that if the address field contains:


Number Instruction
Bin Hex 000000000000 (00016), then a skip will occur
0001 1 Load X if AC < 0. This refers to the assembly
instruction Skipcond 000.
0010 2 Store X
0011 3 Add X
010000000000 (40016), then a skip will occur
0100 4 Subt X if AC = 0. This refers to the assembly
0101 5 Input instruction Skipcond 400.
0110 6 Output
0111 7 Halt 100000000000 (80016), then a skip will occur
if AC > 0. This refers to the assembly
1000 8 Skipcond
instruction Skipcond 800.
1001 9 Jump X

Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE
• Jump X (Load the value of X into PC)

Instruction 100  LOAD 39F


Number Instruction 101  ADD 85D
Bin Hex
102 STORE 477
0001 1 Load X
103 JUMP 200
0010 2 Store X
0011 3 Add X 104 LOAD 135

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.

So the next instruction to be executed is at


address A3516.
Assembly Language
MARIE INSTRUCTION SET ARCHITECTURE

• The architecture and the instruction set of MARIE is


kept as simple as possible and yet it conveys the
information necessary to understand how a
computer works.

• Therefore, several useful instructions were omitted.


However, this instruction set is still quite powerful.

• Once familiarity with how the machine works is


achieved, the instruction set will be extended to
make programming easier.

Assembly Language
REGISTER TRANSFER LANGUAGE

• Each of the instructions actually consists of a sequence of


smaller instructions called micro-operations.

• The exact sequence of micro-operations that are carried out


by an instruction can be specified using register transfer
language (RTL).

• In the MARIE RTL, the following notations are used:

‒ M[X] indicates the actual data value stored in memory


location X
‒  to indicate the transfer of bytes to a register or
memory location.

Assembly Language
REGISTER TRANSFER LANGUAGE

• Load X

0 0 0 1 X
15 12 11 0

This instruction loads the contents of memory location X into the


AC.

RTL for Load X:

MAR  X
MBR  M[MAR]
AC  MBR

Assembly Language
REGISTER TRANSFER LANGUAGE

RTL for Load X:

MAR  X The address X is loaded into the MAR in


order to send it to the memory.

The data in the memory


MBR  M[MAR] pointed to by MAR (which
contains the address X) is
M[X] transferred to the MBR.

The contents of MBR (which is the


AC  MBR data at memory location X) are
transferred to the AC.

Assembly Language
REGISTER TRANSFER LANGUAGE

• Store X

0 0 1 0 X
15 12 11 0

This instruction stores the contents of the AC in memory location X.

RTL for Store X:

MAR  X
MBR  AC
M[MAR]  MBR

Assembly Language
REGISTER TRANSFER LANGUAGE

RTL for Store X:

MAR  X The address X is loaded into the MAR in


order to send it to the memory.

MBR  AC The data in AC is transferred to the MBR


in order to send it to the memory.

M[X] The contents of the MBR (which is


M[MAR]  MBR the data at AC) are transferred to
the memory whose address is
specified by MAR (which is X).
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.

RTL for Add X:

MAR  X
MBR  M[MAR]
AC  AC + MBR

Assembly Language
REGISTER TRANSFER LANGUAGE

• Subt X

0 1 0 0 X
15 12 11 0

The data value stored at address X is subtracted from the AC and


the result is stored back to the AC.

RTL for Subt X:

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.

RTL for Input:

AC  InREG

Assembly Language
REGISTER TRANSFER LANGUAGE

• Output

0 1 1 0 0 0 0...0
15 12 11 0

This instruction causes the contents of the AC to be placed into the


OutREG, where it is eventually sent to the output device.

RTL for Output:

OutREG  AC

Assembly Language
REGISTER TRANSFER LANGUAGE

• Halt

0 1 1 1 0 0 0...0
15 12 11 0

No operations are performed on registers; the machine simply


ceases execution.

Assembly Language
REGISTER TRANSFER LANGUAGE

• Skipcond

1 0 0 0 000...0
15 12 11 10 9 0

This instruction uses the bits in positions 10 and 11 in the address


field to determine what comparison to perform on the AC.

Depending on this bit combination, the AC is checked to see


whether it is negative, equal to zero, or greater than zero.

If the given condition is true, then the next instruction is skipped.


This is performed by incrementing the PC register by 1.

Assembly Language
REGISTER TRANSFER LANGUAGE

• Skipcond

1 0 0 0 000...0
15 12 11 10 9 0

RTL for Skipcond:

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

This instruction causes an unconditional branch to the given


address, X. Therefore, to execute this instruction, X must be loaded
into the PC.

RTL for Jump X:

PC  X

Assembly Language
DISASSEMBLING MARIE INSTRUCTIONS

• Disassemble the instruction 0011000000001101:

Opcode = 0011 (Add X)

X = 000000001101
= 00D16
Add the contents of memory location 00D16 to the contents
of the AC and store the results back to the AC.

So the assembly instruction is Add 00D.

Assembly Language
DISASSEMBLING MARIE INSTRUCTIONS

• Disassemble the instruction 21E216:

21E216 = 0010000111100010

Opcode = 0010 (Store X)

X = 000111100010
= 1E216
Store the contents of the AC to memory location 1E216.

So the assembly instruction is Store 1E2.

Assembly Language
INSTRUCTION PROCESSING

• The fetch-decode-execute cycle represents the steps that a


computer follows to run a program.

• In the cycle, the CPU:


1. fetches an instruction (transfers it from main memory to
the instruction register),
2. decodes it (determines the opcode and fetches any data
necessary to carry out the instruction),
3. and executes it (performs the operation(s) indicated by
the instruction).

• Recall that when a program is initially loaded, the address


of the first instruction must be placed in the PC.

Assembly Language
INSTRUCTION PROCESSING
• The steps in this cycle, which take place in specific clock cycles, are
listed below:

1. Copy the contents of the PC to the MAR:


send the address of the instruction
MAR  PC to be executed to the main memory

2. Go to main memory and fetch the instruction found at the


address in the MAR, placing this instruction in the IR; increment
PC by 1 (PC now points to the next instruction in the program):

IR  M[MAR] instruction is stored in IR


PC  PC + 1

Take note that Steps 1 and 2 make up the fetch phase of the cycle.

Assembly Language
INSTRUCTION PROCESSING

3. Decode the leftmost four bits to determine the opcode


and copy the rightmost 12 bits (the address field) of the
IR into the MAR (to prepare for any operand fetch)

Decode IR[15–12]
MAR  IR[11–0] send the address field to MAR to
prepare for any operand fetch

This is the decode phase of the cycle.

IR

Assembly Language
INSTRUCTION PROCESSING

4. If necessary, use the address in the MAR to go to


memory to get data, placing the data in the MBR (and
possibly the AC), and then execute the instruction.

MBR  M[MAR]
Execute the actual instruction

This is the execute phase of the cycle.

• Note that computers today, even with large instruction sets,


long instructions, and huge memories, can execute millions
of these fetch-decode-execute cycles in the blink of an eye.

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

• Flowchart of the fetch-decode-execute cycle of MARIE.

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

• This program adds two numbers together (both of which


are found in main memory), storing the sum in memory.

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 list of instructions under


the Instruction column
constitutes the actual
assembly language program.

• 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.

• For simplicity, assume the • It is often easier for humans to read


programs in MARIE are hexadecimal as opposed to binary, so the
always loaded starting at actual contents of memory are displayed
address 100 (in hex). in hexadecimal.

Assembly Language
INSTRUCTION PROCESSING

• The first instruction is Load 104.

This loads the contents of memory


location 104 into the AC.

Since of contents of memory location


104 is 002316, it will be loaded into
the AC.

Step RTN PC IR MAR MBR AC


(initial values) 100 --- --- --- ---
Fetch MAR PC 100 --- 100 --- ---
IR M[MAR] 100 1104 100 --- ---
PC PC + 1 101 1104 100 --- ---
Decode (Decode IR[15–12]) 101 1104 100 --- ---
MAR  IR[11–0] 101 1104 104 --- ---
Get Operand MBR  M[MAR] 101 1104 104 0023 ---
Execute AC  MBR 101 1104 104 0023 0023

Assembly Language
INSTRUCTION PROCESSING
• The Add 105 instruction adds the
contents of memory location 105 to
the AC and store the results into AC.

It therefore adds FFE916 (2310) that it


finds at address 105 to 002316
resulting to 000C16.

000C16 is then stored to the AC.

Step RTN PC IR MAR MBR AC


(initial values) 101 1104 104 0023 0023
Fetch MAR PC 101 1104 101 0023 0023
IR M[MAR] 101 3105 101 0023 0023
PC PC + 1 102 3105 101 0023 0023
Decode (Decode IR[15–12]) 102 3105 101 0023 0023
MAR  IR[11–0] 102 3105 105 0023 0023
Get Operand MBR  M[MAR] 102 3105 105 FFE9 0023
Execute AC  AC + MBR 102 3105 105 FFE9 000C

Assembly Language
INSTRUCTION PROCESSING

• The Store 106 instruction stores


the contents of the AC to
memory location 106.

It therefore stores 000C16,the


contents of the AC, to memory
location 106.

Step RTN PC IR MAR MBR AC


(initial values) 102 3105 105 FFE9 000C
Fetch MAR PC 102 3105 102 FFE9 000C
IR M[MAR] 102 2106 102 FFE9 000C
PC PC + 1 103 2106 102 FFE9 000C
Decode (Decode IR[15–12]) 103 2106 102 FFE9 000C
MAR  IR[11–0] 103 2106 106 FFE9 000C
Get Operand not necessary 103 2106 106 FFE9 000C
Execute MBR  AC 103 2106 106 000C 000C
M[MAR]  MBR 103 2106 106 000C 000C

Assembly Language
INSTRUCTION PROCESSING

• The Halt instruction simply


signals the end of the program.

Step RTN PC IR MAR MBR AC


(initial values) 103 2106 106 000C 000C
Fetch MAR PC 103 2106 103 000C 000C
IR M[MAR] 103 7000 103 000C 000C
PC PC + 1 104 7000 103 000C 000C
Decode (Decode IR[15–12]) 104 7000 103 000C 000C
MAR  IR[11–0] 104 7000 000 000C 000C
Get Operand not necessary 104 7000 000 000C 000C
Execute Stop Execution 104 7000 000 000C 000C

Assembly Language
ASSEMBLERS

• An assembler is a program that will convert assembly


language (using mnemonics) into machine language (which
consists entirely of binary values, or strings of zeros and
ones).

• Assemblers take a programmer's assembly language


program, which is really a symbolic representation of the
binary numbers, and convert it into binary instructions, or
the machine code equivalent.

• The assembler reads a source file (assembly program) and


produces an object file (the machine code).

Assembly Language
ASSEMBLERS

• Substituting simple alphanumeric names for the opcodes


makes programming much easier (such as Load instead of
0001).

• Labels (simple names) can also be used to identify or name


particular memory addresses, making the task of writing
assembly programs even simpler.

• For example, in the program to add two numbers, labels


can be used to indicate the memory addresses, thus making
it unnecessary to know the exact memory address of the
operands for instructions.

Assembly Language
ASSEMBLERS

Address Instruction Address Instruction


100 Load 104 100 Load 104
X
101 Add 105 101 Add 105
Y
102 Store 106 102 Store Z
106
103 Halt 103 Halt
104 0023 104 X, 0023
105 FFE9 105 Y, FFE9
106 0000 106 Z, 0000

Assembly Language
ASSEMBLERS

• The assembly process normally needs two passes:

1. During the first pass, the assembler assembles as


much of the program it can, while it builds a
symbol table that contains memory references
for all symbols in the program.

2. During the second pass, the instructions are


completed using the values from the symbol
table.

Assembly Language
ASSEMBLERS

• Consider the sample program given.

During the first pass, a symbol table will


be created.

It will also begin translating instructions


although after the first pass, the
translated instructions will be incomplete.

Partial 1
Translation 3 X 104
Symbol
of 2 Table Y 105
Instructions 7 000 Z 106

Assembly Language
ASSEMBLERS

• On the second pass, the assembler


uses the symbol table to fill in the
addresses and create the
corresponding machine language
instructions.

Complete 1 104
Translation 3 105 X 104
Symbol
of 2 106 Table Y 105
Instructions 7 000 Z 106

Assembly Language
ASSEMBLERS

• Because most people are uncomfortable reading


hexadecimal, most assembly languages allow the data
values stored in memory to be specified as binary,
hexadecimal, or decimal.

• Typically, some sort of assembler directive (an instruction


specifically for the assembler that is not supposed to be
translated into machine code) is given to the assembler to
specify which base is to be used to interpret particular
value.

• In MARIE's assembly language, DEC is used for decimal and


HEX is for hexadecimal.

Assembly Language
ASSEMBLERS

Address Instruction Address Instruction


100 Load 104 100 Load X
101 Add 105 101 Add Y
102 Store 106 102 Store Z
103 Halt 103 Halt
104 0023 104 X, DEC
0023 35
105 FFE9 105 DEC
Y, FFE9 -23
106 0000 106 Z, 0000
HEX 0000

Assembly Language
ASSEMBLERS

• Another kind of directive common to virtually every


programming language is the comment delimiter.

• Comment delimiters are special characters that tell


the assembler (or compiler) to ignore all text
following the special character.

• MARIE's comment delimiter is a front slash ("/"),


which causes all text between the delimiter and the
end of the line to be ignored.

Assembly Language
EXERCISE
• Consider the following MARIE assembly language program:

Hex Address Label Instruction


100 Load A
101 Add One 1. List the hexadecimal code for
each instruction.
102 Jump S1
103 S2, Add One
104 Store A 2. What are the contents of the
symbol table for this program?
105 Halt
106 S1, Add A
107 Jump S2 3. Indicate what happens after the
execution of each instruction.
108 A, HEX 0023
109 One, HEX 0001

Assembly Language
MARIE PROGRAMMING

• Example 1: Write an assembly program to


implement the following pseudo code:

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

100 If, Load X /load the value of X to AC so it can be compared


101 Subt Y /AC = AC (X) – Y (compare X and Y)
102 Skipcond 400 /if AC = 0 (X = Y), skip next instruction
103 Jump Else /AC  0 (X  Y), jump to Else statements
104 Then, Load X /load X to AC so it can be doubled
105 Add X /AC = AC(X) + X (add X to itself to double its value
106 Store X /store the new value back to X
107 Halt /terminate program
108 Else, Load Y /load Y to AC so subtraction can be performed
109 Subt X /perform Y – X and store results to AC
10A Store Y /store results back to Y
10B Halt /terminate program
10C X, DEC 12 /storage for variable X if X = Y then
10D Y, DEC 10 /storage for variable Y X= X*2
else
Solution 1 Y=Y–X

Assembly Language
MARIE PROGRAMMING

100 If, Load X /load the value of X to AC so it can be compared


101 Subt Y /AC = AC (X) – Y (compare X and Y)
102 Skipcond 400 /if AC = 0 (X = Y), skip next instruction
103 Jump Else /AC  0 (X  Y), jump to Else statements
104 Then, Load X /load X to AC so it can be doubled
105 Add X /AC = AC(X) + X (add X to itself to double its value
106 Store X /store the new value back to X
107 Halt Endif
Jump /terminate
/jump to end
program
of program
108 Else, Load Y /load Y to AC so subtraction can be performed
109 Subt X /perform Y – X and store results to AC
10A Store Y /store results back to Y
10B Endif, Halt /terminate program
10C X, DEC 12 /storage for variable X if X = Y then
10D Y, DEC 10 /storage for variable Y X= X*2
else
Solution 2 Y=Y–X

Assembly Language
MARIE PROGRAMMING

100 If, Load X 100 110C Instruction


Number Instruction
101 Subt Y 101 410D
102 Skipcond 400 102 8400 Bin Hex
103 Jump Else 103 9108 0001 1 Load X
104 Then, Load X 104 110C 0010 2 Store X
105 Add X 105 310C 0011 3 Add X
106 Store X 106 210C 0100 4 Subt X
107 Jump Endif 107 910B 0101 5 Input
108 Else, Load Y 108 110D 0110 6 Output
109 Subt X 109 410C 0111 7 Halt
10A Store Y 10A 210D 1000 8 Skipcond
10B Endif, Halt 10B 7000 1001 9 Jump X
10C X, DEC 12 10C 000C
10D Y, DEC 10 10D 000A

Assembly Language
MARIE PROGRAMMING

• Example 2: Write an assembly


program to implement the following:

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

100 If, Load I /load value of I to AC so it can be compared


101 Subt Ten /Subtract 10 from I (compare I and 10)
102 Skipcond 800 /if AC > 0 (I > 10), skip next instruction (do Else)
103 Jump Then /AC ≤ 0, perform Then statements
104 Else, Load One /make AC = 1 so it can be loaded to Sum
105 Store Sum /Store AC (1) to Sum (Sum = 1)
106 Jump End /jump to end of program
107 Then, Load Zero /AC = 0
108 Store Sum /store AC (0) to Sum (Sum = 0)
109 End, Halt /terminate program
10A Sum, DEC 0 /storage for the variable Sum
10B I, DEC 0 /storage for the variable I
10C Zero, DEC 0 /the constant value 0 if (i < = 10)
10D One, DEC 1 /the constant value 1 sum = 0;
10E Ten, DEC 10 /the constant value 10 else
sum = 1;

Assembly Language
MARIE PROGRAMMING

100 If, Load I 100 110B Instruction


101 Subt Ten 101 410E Number Instruction
102 Skipcond 800 102 8800 Bin Hex
103 Jump Then 103 9107 0001 1 Load X
104 Else, Load One 104 110D 0010 2 Store X
105 Store Sum 105 210A 0011 3 Add X
106 Jump End 106 9109 0100 4 Subt X
107 Then, Load Zero 107 110C 0101 5 Input
108 Store Sum 108 210A
0110 6 Output
109 End, Halt 109 7000
0111 7 Halt
10A Sum, DEC 0 10A 0000
1000 8 Skipcond
10B I, DEC 0 10B 0000
1001 9 Jump X
10C Zero, DEC 0 10C 0000
10D One, DEC 1 10D 0001
10E Ten, DEC 10 10E 000A

Assembly Language
MARIE PROGRAMMING

• Example 3: Write an assembly


program to implement the
following:

x = 1;
while (x < 10)
x = x + 1;

Assembly Language
MARIE PROGRAMMING

x = 1; Load One AC = One


while (x < 10)
x = x + 1;
Store X X = AC
Loop, Load X AC = X
Subt Ten AC = AC – Ten

Skipcond 000 If AC < 0, skip


Jump End If AC >= 0
Load X AC = X
Add One AC = AC + One

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

100 Load One /load 1 to AC so it can loaded to X


101 Store X /initialize X to 1
102 Loop, Load X /get the current value of X to compare it to 10
103 Subt Ten /compare X to 10
104 Skipcond 000 /If AC < 0 (X < 10), continue loop
105 Jump End /If AC is not < 0 (X is not < 10), end loop
106 Load X /execute loop, get value of X to increment it
107 Add One /add 1 to X to increment it
108 Store X /store updated value back to X
109 Jump Loop /continue loop by checking if X is less than 10
10A End, Halt /terminate program
10B X, DEC 0 /storage for X
10C One, DEC 1 /constant value 1 x = 1;
10D Ten, DEC 10 /constant value 10 while (x < 10)
x = x + 1;

Assembly Language
EXERCISES

1. Write a MARIE assembly program that stores a grade based on


the score of a test.

Score < 70, Grade = 5


Score >= 70 and < 80, Grade = 3
Score >= 80 and < 90, Grade = 2
Score >= 90, Grade = 1

2. Write a MARIE assembly program that does the following:

sum = 0;
for (i = 0; i <= n; i++)
sum = sum + i;

Assembly Language
EXERCISES

3. Write a MARIE assembly program that multiplies two


numbers, X and Y. The product will then be stored in a
variable called PROD.

4. Write a MARIE assembly program that divides a number


X by another number Y. The quotient is then stored in
variable called QUOT.

Assume that X is exactly divisible by Y.

Assembly Language

You might also like