CE222_Slides7
CE222_Slides7
Salman Ashraf
• Program:
A collection of instructions and operands for directing the
computer to perform a required data processing task.
• 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
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
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
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
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
- 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
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
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
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
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
P0
ORG 0
LOP, CLE / Clear E E0
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 PP+X
ZRO, LDA X / Load multiplicand E0
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
ORG 0
X: HEX 5
Y: HEX A
Z: HEX 0
TMP: HEX 0
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
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.
Serial Computer
Input/Output communication
terminal registers and
interface
flip-flops
Receiver
Printer interface OUTR FGO
AC
Transmitter
Keyboard interface INPR FGI
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
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