0% found this document useful (0 votes)
2 views

CE222_Slides7

The document provides an overview of computer organization and assembly language, detailing different types of programming languages, including machine, assembly, and high-level languages. It includes comparisons of programming languages, basic computer instruction sets, and examples of assembly language programs for arithmetic operations. Additionally, it discusses the syntax of assembly language and the implementation of loops and arithmetic operations in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CE222_Slides7

The document provides an overview of computer organization and assembly language, detailing different types of programming languages, including machine, assembly, and high-level languages. It includes comparisons of programming languages, basic computer instruction sets, and examples of assembly language programs for arithmetic operations. Additionally, it discusses the syntax of assembly language and the implementation of loops and arithmetic operations in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

CE222

Computer Organization and


Assembly Language

Salman Ashraf

Faculty of Computer Science and Engineering


PROGRAMMING LANGUAGES

• Program:
A collection of instructions and operands for directing the
computer to perform a required data processing task.

• Various types of programming languages:

• Machine Language
- Binary or Hexadecimal code

• Assembly Language
- Symbolic code (Mnemonics)
- Translation is done by Assembler

• High-level Language
- Translation is done by Complier
COMPARISON OF PROGRAMMING LANGUAGES
int A, B, C;
• How to add two numbers using C++? A = 3;
B = 5;
C = A + B;
Instruction Set of 3-bit Processor

• How are the numbers actually added by CPU?


• Example: 3-bit Processor

• Equivalent program for 3-bit Processor


Mnemonics or Symbolic code Machine Language or Binary code
Location Instruction Location Instruction
0 LDD 4 0 00100100
1 ADD 5 1 10000101
2 STD 6 2 01100110
3 STP 3 00000000
4 3 (1st operand) 4 00000011 (1st operand)
5 5 (2nd operand) 5 00000101 (2nd operand)
6 6
BASIC COMPUTER INSTRUCTION SET
Hex Code
Symbol I=0 I=1 Description
AND 0xxx 8xxx AND memory word to AC
ADD 1xxx 9xxx Add memory word to AC
LDA 2xxx Axxx Load AC from memory
STA 3xxx Bxxx Store content of AC into memory
BUN 4xxx Cxxx Branch unconditionally
BSA 5xxx Dxxx Branch and save return address
ISZ 6xxx Exxx Increment and skip if zero

CLA 7800 Clear AC


CLE 7400 Clear E
CMA 7200 Complement AC
CME 7100 Complement E
CIR 7080 Circulate right AC and E
CIL 7040 Circulate left AC and E
INC 7020 Increment AC
SPA 7010 Skip next instr. if AC is positive
SNA 7008 Skip next instr. if AC is negative
SZA 7004 Skip next instr. if AC is zero
SZE 7002 Skip next instr. if E is zero
HLT 7001 Halt computer

INP F800 Input character to AC


OUT F400 Output character from AC
SKI F200 Skip on input flag
SKO F100 Skip on output flag
ION F080 Interrupt on
IOF F040 Interrupt off
COMPARISON OF PROGRAMMING LANGUAGES
• Basic Computer Machine Language program to add
• Hexadecimal program
two numbers
Location Instruction
Location Instruction Code
000 2004
0 0010 0000 0000 0100 Load from
001 1005
1 0001 0000 0000 0101 address 100
002 3006
10 0011 0000 0000 0110 003 7001
11 0111 0000 0000 0001 LDA 004 3
100 0000 0000 0000 0011 005 5
101 0000 0000 0000 0101 006 0000
110 0000 0000 0000 0000

• Basic Computer program using Mnemonics or Symbolic code


Location Instruction Comments
000 LDA 004 Load 1st operand into AC
001 ADD 005 Add 2nd operand to AC
002 STA 006 Store sum in location 006
003 HLT Halt computer
004 3 1st operand
005 5 2nd operand
006 0000 Store sum here

• Assembly Language program (note variable names instead of addresses)


ORG 0 /Origin of program is location 0
LDA A /Load operand from location A
ADD B /Add operand from location B
STA C /Store sum in location C
HLT /Halt computer
A, DEC 3 /Decimal operand
B, DEC 5 /Decimal operand
C, DEC 0 /Sum stored in location C
END /End of symbolic program
ASSEMBLY LANGUAGE
Syntax of Basic Computer Assembly Language:
Each line is arranged in three columns, called fields.
• Label field
- May be empty or may specify a symbolic address.
- Consists of up to 3 characters.
- Terminated by a comma.
• Instruction field
- Specifies a machine instruction or a pseudo instruction
- May specify one of:
• Memory-reference instruction (MRI).
MRI consists of two or three symbols separated by spaces.
ADD A (direct address MRI)
ADD A I (indirect address MRI)
• Register-reference or Input/Output instruction.
Non-MRI does not have an address part.
• Pseudo instruction with or without an operand.
• Comment field
- May be empty or may include a comment.
PSEUDO INSTRUCTIONS

ORG N: Hexadecimal number N is the memory location for


the instruction or operand listed in the following line.

END: Denotes the end of symbolic program.

DEC N: Signed decimal number N to be converted to the binary.

HEX N: Hexadecimal number N to be converted to the binary.


SIMULATING THE BASIC COMPUTER

• Students must use the Basic Computer Simulator to


practice Assembly Language Programming.
– The Basic Computer Simulator is the courtesy of MIT Licence.

• Please use the simulator_basic_comp.swf file


uploaded to MS Teams to access the simulator.
– The uploaded “flash projector” may be used to open the .swf file.
» Press Ctrl+F to view all registers, flip-flops and memory.
– Alternatively, use your web browser to open the .swf file.
DIFFERENCE IN SYNTAX

• Note the following differences in the syntax of the actual


assembly language (given in the book) and the one used in
the simulator:
– A maximum of three characters are allowed in the label field in the
book, whereas, in the simulator, the number of characters can be
large.
– The label field is terminated with “,” in the book, whereas in the
simulator “:” is used.
– The comment field in the book starts with “/”, whereas in the
simulator it starts with “//”.
BASIC COMPUTER INSTRUCTION SET
Hex Code
Symbol I=0 I=1 Description
AND 0xxx 8xxx AND memory word to AC
ADD 1xxx 9xxx Add memory word to AC
LDA 2xxx Axxx Load AC from memory
STA 3xxx Bxxx Store content of AC into memory
BUN 4xxx Cxxx Branch unconditionally
BSA 5xxx Dxxx Branch and save return address
ISZ 6xxx Exxx Increment and skip if zero
Q. There is no instruction
available in the Basic CLA 7800 Clear AC
CLE 7400 Clear E
Computer for subtraction, CMA 7200 Complement AC
so how are we going to CME 7100 Complement E
CIR 7080 Circulate right AC and E
perform subtraction? CIL 7040 Circulate left AC and E
INC 7020 Increment AC
SPA 7010 Skip next instr. if AC is positive
SNA 7008 Skip next instr. if AC is negative
A. Through a program. SZA 7004 Skip next instr. if AC is zero
SZE 7002 Skip next instr. if E is zero
HLT 7001 Halt computer

INP F800 Input character to AC


OUT F400 Output character from AC
SKI F200 Skip on input flag
SKO F100 Skip on output flag
ION F080 Interrupt on
IOF F040 Interrupt off
SUBTRACT USING ASSEMBLY LANGUAGE

Assembly Language program of Basic Computer to subtract two numbers:

ORG 100 / Origin of program is location 100


LDA SUB / Load subtrahend to AC
CMA / Complement AC
INC / Increment AC
ADD MIN / Add minuend to AC
STA DIF / Store difference
HLT / Halt computer
MIN, DEC 5 / Minuend
SUB, DEC 3 / Subtrahend
DIF, DEC 0 / Difference stored here
END / End of symbolic program
TRANSLATION TO HEXADECIMAL
Hex Code
Symbol I=0 I=1 Description
AND 0xxx 8xxx AND memory word to AC
ADD 1xxx 9xxx Add memory word to AC
LDA 2xxx Axxx Load AC from memory
Hexadecimal Code Assembly Language STA 3xxx Bxxx Store content of AC into memory
Program BUN 4xxx Cxxx Branch unconditionally
Location Content BSA 5xxx Dxxx Branch and save return address
ORG 100 ISZ 6xxx Exxx Increment and skip if zero
100 2107 LDA SUB CLA 7800 Clear AC
101 7200 CMA CLE 7400 Clear E
102 7020 INC CMA 7200 Complement AC
103 1106 ADD MIN CME 7100 Complement E
104 3108 STA DIF CIR 7080 Circulate right AC and E
CIL 7040 Circulate left AC and E
105 7001 HLT INC 7020 Increment AC
106 0005 MIN, DEC 5 SPA 7010 Skip next instr. if AC is positive
107 0003 SUB, DEC 3 SNA 7008 Skip next instr. if AC is negative
108 0000 DIF, HEX 0 SZA 7004 Skip next instr. if AC is zero
END SZE 7002 Skip next instr. if E is zero
HLT 7001 Halt computer

INP F800 Input character to AC


OUT F400 Output character from AC
SKI F200 Skip on input flag
SKO F100 Skip on output flag
ION F080 Interrupt on
IOF F040 Interrupt off
PROGRAM LOOPS
Loop: A sequence of instructions that are executed a desired number
of times, each time with a different set of data.

C++ program to add 3 numbers using a loop:


int main()
{

int A[3]={1,4,3}; // Array of operands


int sum=0; // Accumulator
int i=0; // Loop counter

do
{
sum=sum + A[i];
i++;
}
while (i<3);
cout << sum;
}
PROGRAM LOOPS
Symbolic program (in the memory of Basic Computer) to add 3 numbers using a loop:
Mem
Loc Content Comments
0 CLA / Clear AC
1 ADD 7 I / Add (indirectly) operand whose effective address is at location 7
2 ISZ 7 / Increment contents of location 7
3 ISZ 8 / Increment loop counter & skip next instruction if counter is zero
4 BUN 1 / Branch to location 1
5 STA 9 / Store answer of sum to location 12
6 HLT / End program
7 A / Pointer to the address of operand
8 -3 / Loop counter
9 /Answer of sum is stored here
A 1 /1st operand
B 4 /2nd operand
C 3 /3rd operand
PROGRAM LOOPS

Assembly Language program to add 3 numbers using a loop:

Memory location
ORG 0 / Origin of program is HEX location 0
0 CLA / Clear AC
1 LOP, ADD PTR I / Add an operand to AC “indirectly”
2 ISZ PTR / Increment pointer
Loop
3 ISZ CTR / Increment counter
4 BUN LOP / Repeat loop again
5 STA SUM / Store sum
6 HLT / End of instructions
Pointer & 7 PTR, HEX A / Pointer to the address of operand
Loop counter 8 CTR, DEC -3 / Initial value for the loop counter
9 SUM, DEC 0 / Sum is stored here
ORG A / Origin of operands is HEX location A
A DEC 1 / First operand
Operands B DEC 4 / Second operand
C DEC 3 / Third operand
END / End of program
PROGRAM LOOPS

C++ program to add 100 numbers:

int main()
{

int A[100]={1,2,3,…100};
int sum=0, i=0;

do
{
sum=sum + A[i];
i++;
}
while (i<100);
cout << sum;
}
PROGRAM LOOPS

Assembly Language program to add 100 numbers:

Memory location
ORG 0 / Origin of program is HEX location 0
0 CLA / Clear AC
1 LOP, ADD PTR I / Add an operand to AC “indirectly”
2 ISZ PTR / Increment pointer
Loop
3 ISZ CTR / Increment counter
4 BUN LOP / Repeat loop again
5 STA SUM / Store sum
6 HLT / End of instructions
Pointer & 7 PTR, HEX A / Pointer to the address of operand
Loop counter 8 CTR, DEC -100 / Initial value for the loop counter
9 SUM, DEC 0 / Sum is stored here
ORG A / Origin of operands is HEX location A
A DEC 1 / First operand
. . /.
Operands .
. . /.
. DEC 100 / Last operand
END / End of program
PROGRAMMING ARITHMETIC AND LOGIC OPERATIONS

Implementation of Arithmetic Operations

- Hardware Implementation
- Implementation of an operation in a computer with one
machine instruction

- Software Implementation
- Implementation of an operation with a program using
machine instruction set
- Usually when the operation is not included in the
instruction set

Software Implementation examples for Basic Computer:


- Subtraction
- Multiplication
BASIC COMPUTER INSTRUCTIONS
Hex Code
Symbol I=0 I=1 Description
AND 0xxx 8xxx AND memory word to AC
ADD 1xxx 9xxx Add memory word to AC
LDA 2xxx Axxx Load AC from memory
STA 3xxx Bxxx Store content of AC into memory
BUN 4xxx Cxxx Branch unconditionally
BSA 5xxx Dxxx Branch and save return address
ISZ 6xxx Exxx Increment and skip if zero

CLA 7800 Clear AC


CLE 7400 Clear E
CMA 7200 Complement AC
CME 7100 Complement E
CIR 7080 Circulate right AC and E
CIL 7040 Circulate left AC and E
INC 7020 Increment AC
SPA 7010 Skip next instr. if AC is positive
SNA 7008 Skip next instr. if AC is negative
SZA 7004 Skip next instr. if AC is zero
SZE 7002 Skip next instr. if E is zero
HLT 7001 Halt computer

INP F800 Input character to AC


OUT F400 Output character from AC
SKI F200 Skip on input flag
SKO F100 Skip on output flag
ION F080 Interrupt on
IOF F040 Interrupt off
MULTIPLICATION PROGRAM USING LOOPS
Example: 15 x 11 = 165 X= (15)10
Y= (11)10

One way of multiplication can be to add multiplicand 15, 11 times (15+15+15+15+15+15+15+15+15+15+15)

Program for Basic Computer


Program for 3-bit Opcode Processor
Run “7_multiplication_using_loop_2.tbt” program
ORG 0
LOOP: LDA Prod // Load Product to AC
ADD X // ADD X to AC
STA Prod // Store AC in Prod
ISZ CTR // Increment CRT and Skip Next Inst. if CRT=0
BUN LOOP // Jump to LOOP
HLT
Prod: DEC 0 // Product is stored here
X: DEC 15 // Multiplicand
CTR: DEC -11 // Loop counter (Multiplier)
END

To test this Basic Computer program, use the file “Multiplication


using Successive Additions”.

Although the above programs achieve the correct answer but with increased computationally complexity.
- Programs on this slide compute the same answer (by adding 15 eleven times) in 11 loops.
- We will write another program that computes the answer (15 x 11 = 165) in only 4 loops.
MULTIPLICATION
X holds the multiplicand
Y holds the multiplier

X = (0000 1111)2 = (15)10


Y = (0000 1011)2 = (11)10

This is how we manually multiply two binary numbers, each with four
significant bits:

X = 0000 1111
Y = 0000 1011
0000 1111
0001 1110
0000 0000
0111 1000
1010 0101
MULTIPLICATION USING PARTIAL PRODUCT
X holds the multiplicand
Y holds the multiplier
P holds the partial product

X = (0000 1111)2 = (15)10


Y = (0000 1011)2 = (11)10

This is how we multiply manually using partial product:

X = 0000 1111
Y = 0000 1011 P
0000 1111 0000 0000
0001 1110 0000 1111
0000 0000 0010 1101
0111 1000 0010 1101
1010 0101 1010 0101
MULTIPLICATION USING PARTIAL PRODUCT
X (multiplicand) = 0000 1111
Y (multiplier) = 0000 1011
P (partial product) = 0000 0000

Step by step process of multiplication using partial product


Name of
Comment Value
variable
Start with a partial product equal to zero P0 = 0000 0000
Use the original value of X X= 0000 1111
Because the 1st bit of Y = 1, add X to P0 (P1 = P0 + X) P1 = 0000 1111
Circulate X to left X = CIL(X) = 0001 1110
Because the 2nd bit of Y = 1, add X to P1 (P2 = P1 + X) P2 = 0010 1101
Circulate X to left X = CIL(X) = 0011 1100
As the 3rd bit of Y = 0, partial product remains unchanged (P3 = P2) P3 = 0010 1101
Circulate X to left X = CIL(X) = 0111 1000
Because the 4th bit of Y = 1, add X to P3 (P4 = P3 + X) P4 = 1010 0101
P4 is the final product
FLOWCHART FOR MULTIPLICATION PROGRAM
CTR  - 4
P0 X holds the multiplicand
Y holds the multiplier
E0 P holds the partial product

AC  Y Example with four significant bits

Check the X = 0000 1111 P


cir EAC bit of Y Y = 0000 1011 0000 0000
0000 1111 0000 1111
Y  AC 0001 1110 0010 1101
0000 0000 0010 1101
0111 1000 1010 0101
=0 E =1
1010 0101
PP+X Add X to P if
the bit in Y=1
E0
The process of multiplication
AC  X using partial product:

cil EAC
cil X is shifted left 1. Start with partial product P=0.
2. Check the bit of Y.
X  AC
3. Add X to P if the bit of Y is 1.
CTR  CTR + 1 4. Circulate left the value of X.
Repeat
5. Repeat the steps 2 to 4 until
 0_ =0 all bits of Y have been checked.
CTR Stop
PROGRAM FOR MULTIPLICATION USING PARTIAL PRODUCT
CTR  - 4
P0

ORG 0
LOP, CLE / Clear E E0
LDA Y / Load multiplier
CIR / Transfer multiplier bit to E AC  Y
STA Y / Store shifted multiplier
SZE / Check if bit is zero cir EAC
BUN ONE / E=1; go to Label ONE
BUN ZRO / E=0; go to Label ZRO Y  AC
ONE, LDA X / Load multiplicand
ADD P / Add to partial product =0 E =1
STA P / Store partial product
CLE / Clear E PP+X
ZRO, LDA X / Load multiplicand E0
CIL / Shift left
STA X / Store shifted multiplicand
ISZ CTR / Increment counter AC  X
BUN LOP / Counter not zero; repeat loop
HLT / Counter is zero; halt cil EAC
cil
P, DEC 0 / Product formed here
X, DEC 15 / Multiplicand stored here X  AC
Y, DEC 11 / Multiplier stored here
CTR, DEC -4 / This location serves as a counter CTR  CTR + 1
END
 0_ CTR = 0_______
Stop
To test this Basic Computer program, use the file “Multiplication using Partial Product”.
DOUBLE PRECISION ADDITION
An operand stored in two consecutive memory words is called a double
precision operand.
ORG 0 / Origin of Program in RAM is Location 0
AL: HEX FFFF / Low order bits of A
AH: HEX F / High Order bits of A
BL: HEX FFFF / Low order bits of B
BH: HEX F / High order bits of B
CL: HEX 0 / Low order bits of the answer C
CH: HEX 0 / High order bits of answer C

LDA AL / Load A low


ADD BL / Add B low (we get a carry in E)
STA CL / Store AC in C low
CLA / Clear AC
CIL / Circulate left to bring carry into AC(0)
ADD AH / Add A high and carry
ADD BH / Add B high
STA CH / Store AC in C high
HLT / End of Instructions
END / End of Program
To test this Basic Computer program, use the file “Double Precision Addition”.
Programming Logic Operations
BASIC COMPUTER INSTRUCTIONS
Hex Code
Symbol I=0 I=1 Description
AND 0xxx 8xxx AND memory word to AC
ADD 1xxx 9xxx Add memory word to AC
LDA 2xxx Axxx Load AC from memory
STA 3xxx Bxxx Store content of AC into memory
BUN 4xxx Cxxx Branch unconditionally
BSA 5xxx Dxxx Branch and save return address
ISZ 6xxx Exxx Increment and skip if zero

CLA 7800 Clear AC


CLE 7400 Clear E
CMA 7200 Complement AC
CME 7100 Complement E
CIR 7080 Circulate right AC and E
CIL 7040 Circulate left AC and E
INC 7020 Increment AC
SPA 7010 Skip next instr. if AC is positive
SNA 7008 Skip next instr. if AC is negative
SZA 7004 Skip next instr. if AC is zero
SZE 7002 Skip next instr. if E is zero
HLT 7001 Halt computer

INP F800 Input character to AC


OUT F400 Output character from AC
SKI F200 Skip on input flag
SKO F100 Skip on output flag
ION F080 Interrupt on
IOF F040 Interrupt off
LIST OF LOGIC MICROOPERATIONS
Recall from the previous lecture:
- 16 different logic operations with 2 binary variables.

Truth table for 16 functions of 2 variables and the corresponding


16 logic micro-operations:
x 0011
For Basic Computer:
Boolean
y 0101 Function
Operation Name • Green operations are
0000 F0 = 0 F0 Clear implemented directly by Hardware.
0001 F1 = xy FAB AND • Red operations need to be
0010 F2 = xy' F  A  B’ implemented through Software by
0011 F3 = x FA Transfer A using available instructions: AND,
0100 F4 = x'y F  A’  B Complement, etc.
0101 F5 = y FB Transfer B
0110 F6 = x  y FAB Exclusive-OR X Y = X’Y + XY’
0111 F7 = x + y F A B OR X + Y = (X’ Y’)’
1000 F8 = (x + y)' F  (A  B)’ NOR (X + Y)’ = X’ Y’
1001 F9 = (x  y)' F  (A  B)’ Exclusive-NOR (X Y)’ = (X’Y + XY’)’
1010 F10 = y' F  B’ Complement B
1011 F11 = x + y' F A B OR
1100 F12 = x' F  A’ Complement A
1101 F13 = x' + y F  A’  B
1110 F14 = (xy)' F  (A  B)’ NAND (XY)’
1111 F15 = 1 F  all 1s Set to all 1s
SOFTWARE IMPLEMENTATION OF LOGIC OPERATIONS

Example of Software implementation of OR operation in Basic Computer:


From DeMorgan’s Theorem:
Z = X + Y = (X’ Y’)’

ORG 0

X: HEX 5
Y: HEX A
Z: HEX 0
TMP: HEX 0

LDA X / Load 1st operand to AC


CMA / Complement AC to get X’
STA TMP / Store AC in a temporary location
LDA Y / Load 2nd operand Y
CMA / Complement AC to get Y’ To test this
AND TMP / AND AC with X’ to get X’AND Y’ Basic
Computer
CMA / Complement again to get “X OR Y” program,
STA Z / Store the answer “X OR Y” as Z use the file
/ End of Instructions “Program
HLT for OR
END / End of Program Operation”.
Programming Shift Operations
SHIFT MICROOPERATIONS

• There are three types of shifts:


– Logical shift
– Circular shift (Hardware implementation available in Basic Computer)
– Arithmetic shift
BASIC COMPUTER INSTRUCTIONS
Hex Code
Symbol I=0 I=1 Description
AND 0xxx 8xxx AND memory word to AC
ADD 1xxx 9xxx Add memory word to AC
LDA 2xxx Axxx Load AC from memory
STA 3xxx Bxxx Store content of AC into memory
BUN 4xxx Cxxx Branch unconditionally
BSA 5xxx Dxxx Branch and save return address
ISZ 6xxx Exxx Increment and skip if zero

CLA 7800 Clear AC


CLE 7400 Clear E
CMA 7200 Complement AC
CME 7100 Complement E
CIR 7080 Circulate right AC and E
CIL 7040 Circulate left AC and E
INC 7020 Increment AC
SPA 7010 Skip next instr. if AC is positive
SNA 7008 Skip next instr. if AC is negative
SZA 7004 Skip next instr. if AC is zero
SZE 7002 Skip next instr. if E is zero
HLT 7001 Halt computer

INP F800 Input character to AC


OUT F400 Output character from AC
SKI F200 Skip on input flag
SKO F100 Skip on output flag
ION F080 Interrupt on
IOF F040 Interrupt off
SOFTWARE IMPLEMENTATION OF SHIFT OPERATIONS
• Shift operations - Basic Computer has Circular Shift only
- Logical shift-right operation
Implementation
0
CLE
CIR

- Logical shift-left operation


Implementation
0
CLE
CIL

- Arithmetic shift-right operation


sign
Implementation bit

CLE / Clear E
SPA / Skip if AC is positive
CME / Complement E (if AC is negative)
CIR / Circulate E and AC to right
SUBROUTINES IN ASSEMBLY LANGUAGE

• Subroutine: A set of common instructions that can be called in a


main program multiple times.

• Subroutine Linkage: A procedure for branching to a subroutine


and returning to the main program.

• In Basic Computer, branching to a subroutine is done by using a


direct BSA instruction, while returning to the main program in done
through an indirect BUN instruction.
RECALL SUBROUTINE LINKAGE

A direct BSA (Branch and Save Return Address) instruction is used to branch to a subroutine.
M[AR]  PC, PC  AR + 1
Save Return Address Branch

Memory
0
1 0 BSA 9
2 Next instruction

9 2

PC = 9+1 = A Subroutine

1 BUN 9

An indirect BUN (Branch Unconditionally) instruction is used to return to the main program.
PC  AR (indirect memory reference 1 BUN 9)
WRITING A SUBROUTINE IN ASSEMBLY LANGUAGE

Memory
0
1 0 BSA SH3
2 Next instruction

9 2 SH3
Subroutine
A

1 BUN SH3

A Subroutine (SH3)
WRITING A SUBROUTINE IN ASSEMBLY LANGUAGE

Example: A Subroutine (SH3) that shifts the content of AC three times to the left.

Hex address / Main program starts at memory location 0


ORG 0
0 LDA X / Load X
1 BSA SH3 / Branch to subroutine
2 STA X / Store shifted number
3 LDA Y / Load Y
4 BSA SH3 / Branch to subroutine again
5 STA Y / Store shifted number
6 HLT / End of instructions
7 X, DEC 1 / Variable X declared as Dec 1
8 Y, DEC 2 / Variable Y declared as Dec 2

/ Subroutine starts at memory location 9


9 SH3, HEX 0 / Return address is stored here
A CIL / Circulate left
B CIL / Circulate left again
C CIL / Circulate left yet again
D BUN SH3 I / Return to the main program
END
INPUT/OUTPUT CONFIGURATION OF BASIC COMPUTER

A terminal with a keyboard and a printer

Serial Computer
Input/Output communication
terminal registers and
interface
flip-flops

Receiver
Printer interface OUTR FGO

AC

Transmitter
Keyboard interface INPR FGI

Serial Communication Path

INPR Input register - 8 bits


OUTR Output register - 8 bits
FGI Input flag - 1 bit
FGO Output flag - 1 bit
IEN Interrupt enable - 1 bit
INPUT PROGRAM

Program to Input one character (Byte)

CIF, SKI / Check input flag & skip next inst. if FGI=1
BUN CIF / FGI=0, branch to CIF to check again
INP / FGI=1, input character
OUT / Display to ensure correctness
STA CHR / Store character
HLT
CHR, HEX 0 / Store character here
OUTPUT PROGRAM

Program to Output a character

COF, SKO / Check output flag & skip next inst. if FGO=1
BUN COF / FGO=0, branch to COF check again
LDA CHR / Load character into AC
OUT / FGO=1, output character
HLT
CHR, HEX 0057

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy