0% found this document useful (0 votes)
2 views53 pages

COA-Unit 3 ppt

The document provides an overview of basic computer programming concepts, including machine language, assembly language, and the role of assemblers. It discusses program loops, input-output instructions, and provides examples of assembly language programs for various operations such as addition and multiplication. The document also explains how symbolic programs are represented in memory and the process of assembling them into machine code.

Uploaded by

dhruvgandhi824
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 views53 pages

COA-Unit 3 ppt

The document provides an overview of basic computer programming concepts, including machine language, assembly language, and the role of assemblers. It discusses program loops, input-output instructions, and provides examples of assembly language programs for various operations such as addition and multiplication. The document also explains how symbolic programs are represented in memory and the process of assembling them into machine code.

Uploaded by

dhruvgandhi824
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/ 53

Chap - 3

Programming the basic


computer
 Looping
Outline
• Machine Language
• Assembly Language
• Assembler
• Program loops
• Programming Arithmetic and Logic operations
• Subroutines
• I-O Programming
Section - 1
Machine Language
 Program
• A program is a set of instructions or statements for directing
the computer to perform a required data-processing task.
Categories of programs
 Binary code  Octal or hexadecimal code
 This is a sequence of instructions and operands  This is an equivalent translation of the binary
in binary that list the exact representation of code to octal or hexadecimal representation.
instructions as they appear in computer memory.

Location Instruction Code Location Instruction


0 0010 0000 0000 0100 000 2004
1 0001 0000 0000 0101 001 1005
10 0011 0000 0000 0110 002 3006
11 0111 0000 0000 0001 003 7001
100 0000 0000 0101 0011 004 0053
101 1111 1111 1110 1001 005 FFE9
110 0000 0000 0000 0000 006 0000
Categories of programs
 Symbolic code  High-level programming languages
 The user employs symbols (letters, numerals, or  These are special languages developed to reflect
special characters) for the operation part, the the procedures used in the solution of a problem
address part, and other parts of the instruction rather than be concerned with the computer
code. hardware behavior. E.g. Fortran, C++, Java, etc.
 Each symbolic instruction can be translated into  The program is written in a sequence of
one binary coded instruction by a special program statements in a form that people prefer to think in
called an assembler and language is referred to when solving a problem.
as an assembly language program.  However, each statement must be translated into
Location Instruction Comment a sequence of binary instructions before the
program can be executed in a computer.
000 LDA 004 Load first operand into AC
001 ADD 005 Add second operand to AC
INTEGER A, B, C
002 STA 006 Store sum in location 006
DATA A, 83 B,-23
003 HLT Halt computer
C = A + B
004 0053 First operand
END
005 FFE9 Second operand (negative)
006 0000 Store sum here
Section - 2
Assembler
 An assembler is a program that accepts a symbolic language program and produces its binary
machine language equivalent.
 The input symbolic program is called the source program and the resulting binary program is
called the object program.
 The assembler is a program that operates on character strings and produces an equivalent
binary interpretation.
Pseudo Instruction
 A pseudo instruction is not a machine instruction but rather an instruction to the assembler
giving information about some phase of the translation.

Symbol Information for the Assembler


Hexadecimal number N is the memory location for the
ORG N
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 binary.
HEX N Hexadecimal number N to be converted to binary
Basic Computer Instructions
Memory-reference Instruction
Opcode = 000 ~ 110
15 14 12 11 0
I Opcode Address

Register-reference Instruction
15 14 12 11 0
0 1 1 1 Register Operation

Input-Output Instruction
15 14 12 11 0
1 1 1 1 I/O Operation
Memory-reference Instructions
15 14 12 11 0
I Opcode Address

Symbol Code Description

AND 0xxx 8xxx AND memory word to AC


ADD 1xxx 9xxx ADD memory word to AC
LDA 2xxx Axxx Load memory word to AC
STA 3xxx Bxxx Store Content of AC in memory
BUN 4xxx Cxxx Branch Unconditionally
BSA 5xxx Dxxx Branch and Save Return Address
ISZ 6xxx Exxx Increment and Skip if Zero
Register-reference Instructions
15 14 12 11 0
0 1 1 1 Register Operation
Symbol Code Description

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 instruction if AC positive
SNA 7008 Skip next instruction if AC negative
SZA 7004 Skip next instruction if AC zero
SZE 7002 Skip next instruction if E is zero
HLT 7001 Halt Computer
Input-Output Instructions
15 14 12 11 0
1 1 1 1 I/O Operation

Symbol Code Description

INP F800 Input Character to AC (AC(0-7) ➛ INPR)


OUT F400 Output Character from AC (OUTR ➛ AC(0-7))
SKI F200 Skip on input flag
SKO F100 Skip on output flag
ION F080 Interrupt ON
IOF F040 Interrupt OFF
Section - 3
A.L.P. to subtract 2 numbers

Location Instruction Symbol Location


ORG 100 MIN 106
100 LDA SUB SUB 107
101 CMA DIF 108
102 INC
103 ADD MIN
104 STA DIF
105 HLT
106 MIN, DEC 83
107 SUB, DEC -23
108 DIF, HEX 0
END
Representation of Symbolic program in Memory
o Representation of Symbolic Program in Memory user types the symbolic
program on a terminal.
o A loader program is used to input the characters of the symbolic program
into memory.
o Since user inputs symbols, program’s representation in memory uses
alphanumeric characters (8-bit ASCII).
o A line of code is stored in consecutive memory locations with two 8- bit characters in
each location (we have 16-bit wide memory).
o End of line is recognized by the CR code.
Hexadecimal character code
Example: storing the symbolic program in Memory

o PL3, LDA SUB I


o By referring to the ASCII code table, we get:

Memory
Symbol Hex code
word
1 PL 50 4C
2 3, 33 2C
3 LD 4C 44
4 A 41 20
5 SU 53 55
6 B 42 20
7 I CR 49 0D
Conti…
• Each symbol is terminated by the code for space (0x20) except last, which is
terminated by the code of carriage return (0x0D).
• If a line of code has a comment, the assembler recognizes it from code 0x2F
(slash): assembler ignores all characters in the comment field and keeps checking
for a CR code.
• The input for the assembler program is the user’s symbolic language
• program in ASCII.
• The binary program is the output generated by the assembler.
First Pass of an assembler
First pass

LC ← 0

Scan next line of code Set LC


yes
no no
Label ORG
yes
yes
Store address in END
symbol table
no
together with value Go to
of LC second
pass

Increment LC
Second pass

Second Pass
LC ← 0

of an
Scan next line of code Set LC Done
Yes Yes

assembler
Pseudo- Yes No
ORG END
instruction
No
No
No DEC or HEX Convert
Yes MRI operand to
binary and
Get operation Valid non- store in
No
code and set MRI location
bits 2-4 instruction given by LC

Search address- Yes


symbol table for Store binary Error in
binary equivalent of equivalent of line of
symbolic address instruction in code
and set bits 5-16 location given
by LC
Yes No
I
Set first Set first
bit to 1 bit to 0

Assemble all parts of binary


instruction and store in Increment LC
location given by LC
Section - 4
Program Loops
 A program loop is a sequence of instructions that are executed many times, each time with a
different set of data.
 A system program that translates a program written in a high-level programming language to a
machine language program is called a compiler.

Fortran Example (SUM = A(1)+…+A(100))


DIMENSION A(100)
INTEGER SUM, A
SUM = 0
DO 3 J =1,100
3 SUM = SUM + A(J)
A.L.P. to add 100 numbers
1 ORG 100 /Origin of program is HEX 100 13 ADS, HEX 150 /First address of operands
2 LDA ADS /Load first address of operands 14 PTR, HEX 0 /This location reserved for pointer
3 STA PTR /Store in pointer 15 NBR, DEC -100 /Constant to initialized counter
4 LDA NBR /Load minus 100 16 CTR, HEX 0 /This location reserved for a counter
5 STA CTR /Store in counter 17 SUM, HEX 0 /Sum is store here
6 CLA /Clear accumulator 18 ORG 150 /Origin of operands is HEX 150
7 LOP, ADD PTR I /Add an operand to AC 19 DEC 75 /First operand
8 ISZ PTR /Increment pointer .
9 ISZ CTR /Increment counter .
.
10 BUN LOP /Repeat loop again 118 DEC 23 /Last operand
11 STA SUM /Store sum 119 END /End of symbolic program
12 HLT /Halt
A.L.P. to add 100 numbers
A.L.P. to clear the contents of hex locations 500 to 5FF with 0
1 ORG 100 /Origin of program is HEX 100
2 LDA ADS /Load first address of operands
3 STA PTR /Store in pointer
4 LDA NBR /Load minus 255
5 STA CTR /Store in counter
6 CLA /Clear accumulator
7 LOP, STA PTR I /Store zero to location pointed by PTR
8 ISZ PTR /Increment pointer
9 ISZ CTR /Increment counter
10 BUN LOP /Repeat loop again
11 HLT /Halt
12 ADS, HEX 500 /First address of operands
13 PTR HEX 0 /This location reserved for pointer
14 ,
NBR, DEC -255 /Constant to initialized counter
15 CTR HEX 0 /This location reserved for a counter
16 , END /End of symbolic program
Section - 5
A.L.P. to Multiplication program

Specification of Program
Multiplication of 8-bit positive numbers
8-bit x 8-bit = 16-bit Result
Ex: 0000 1111 (15) x 0000 1011 (11)
00001111 x (00000001 + 00000010 + 00001000)
= 00001111 + 00001111 x 2 + 00001111 x 8
= 00001111 + 00001111 << 1 + 00001111 << 3
= 00001111 + 00011110 + 01111000
= 10100101
Multiplication can be implemented with SHIFT
and ADD instructions
CTR  - 8
P0 Example with four significant digits

E0

P
AC  Y X= 0000 1111
Y= 0000 1011 0000 0000 X holds the multiplicand
0000 1111 0000 1111 Y holds the multiplier
cir EAC
0001 1110 0010 1101 P holds the product
0000 0000 0010 1101
Y  AC 0111 1000 1010 0101
1010 0101
=0 =1
E
PP+X

E0

AC  X

cil EAC
cil

X  AC

CTR  CTR + 1

0 =0
CTR Stop
A.L.P. to Multiplication program
ORG 100
LOP, CLE E0 E0 E0
LDA Y AC000B AC0005 AC0002
CIR E1, AC0005 E1, AC0002 E0, AC0001
STA Y Y0005 Y0002 Y0002
SZE
BUN ONE PCONE PCONE
BUN ZRO PCZRO
ONE, LDA X AC000F AC001E
ADD P AC000F+0 AC001E+000F
STA P PAC PAC
CLE E0 E0
ZRO, LDA X AC000F AC001E AC003C
CIL E0, AC001E E0, AC003C E0, AC0078
STA X XAC XAC XAC
ISZ CTR CTR1-8 CTR1-7 CTR1-6
BUN LOP PCLOP PCLOP PCLOP
HLT
CTR, DEC -8 -7 -6 -5
X, HEX 000F 001E 003C 0078
Y, HEX 000B 0005 0002 0001
P, HEX 0 000F 002D 002D
END
A.L.P. to Add Two Double-Precision Numbers

o Double Precision
o A number stored in Two memory word
o Used for great accuracy
o Ex: 00F0FF0F + 00FF00FF = 01F0000E
o Double-precision addition: addition of two 32-bit unsigned integers.
o Added numbers place in two consecutive memory
o locations, AL and AH, and BL and BH.
o Sum is stored in CL and CH:
A.L.P. to Add Two Double-Precision Numbers
00F0FF0F + 00FF00FF = 01F0000E
AL = 1111 1111 0000 1111 (FF0F)
AH = 0000 0000 1111 0000 (00F0)
BL = 0000 0000 1111 1111 (00FF)
BH = 0000 0000 1111 1111 (00FF)
CL = AL+ BL = 1 0000 0000 0000 1110 (1000E)
CL = 0000 0000 0000 1110 (000E)
Carry = 0000 0000 0000 0001 (0001)
AH = 0000 0000 1111 0000 (00F0)
AH+ Carry = 0000 0000 1111 0001 (00F1)
BH = 0000 0000 1111 1111 (00FF)
CH = AH+ carry+ BH = 0000 0001 1111 0000 (01F0)
CH & CL = 01F0 000E
A.L.P. to Add Two Double-Precision Numbers
1 ORG 100 /Origin of program is HEX 100
2 LDA AL /Load A low
3 ADD BL /Add B low, carry in E
4 STA CL /Store in C low
5 CLA /Clear AC
6 CIL /Circulate to bring carry into AC(16)
7 ADD AH /Add A high and carry
8 ADD BH /Add B high
9 STA CH /Store in C high
10 HLT /Halt
Logic Operations

Basic Computer has AND, CMA, CLA


Implementation of OR
DeMorgan’s theorem: A + B = (A’ B’)’

LDA A /ACA
CMA /ACA’
STA TMP /TMPA’
LDA B /ACB
CMA /ACB’
AND TMP /ACB’&A’
CMA /AC(B’&A’)’
Shift Operations

o Basic Computer has CIR, CIL


E AC

o Implementation of Logical Shift


0 AC

shr: CLE shl: CLE


CIR CIL
Logical Shift

• A Left Logical Shift of one position


moves each bit to the left by one.
The vacant least significant bit (LSB)
is filled with zero and the most
significant bit (MSB) is discarded.
• A Right Logical Shift of one
position moves each bit to the right
by one. The least significant bit is
discarded and the vacant MSB is
filled with zero.
Arithmetic Shift

• A Left Arithmetic Shift of one


position moves each bit to the left
by one. The vacant least significant
bit (LSB) is filled with zero and the
most significant bit (MSB) is
discarded. It is identical to Left
Logical Shift.
• A Right Arithmetic Shift of one
position moves each bit to the
right by one. The least significant
bit is discarded and the vacant
MSB is filled with the value of
the previous (now shifted one
position to the right) MSB.
Shift Operations

Implementation of Arithmetic Right Shift

AC

ashr CLE /E0


: SPA /if AC>0 (MSB of AC is 0), Skip
CME /else E1
CIR /Circulate E and AC
Section - 6
Subroutine with example
 A set of common instructions that can
be used in a program many times is
called a subroutine. ORG 100
 Each time that a subroutine is used in 100 LDA X 109 SH4, HEX 0
the main part of the program, a branch is 101 BSA SH4 10A CIL
executed to the beginning of the 102 STA X 10B CIL
subroutine. 103 LDA Y 10C CIL
 After the subroutine has been executed, 104 BSA SH4 10D CIL
a branch is made back to the main 105 STA Y 10E AND MSK
program. 106 HLT 10F BUN SH4 I
107 X, HEX 1234 110 MSK, HEX FFF0
 A subroutine consists of a self
contained sequence of instructions that 108 Y, HEX 4321 END
carries a given task.
Section - 7
Input-Output Programming

o Input-output programs are needed for writing symbols to computer’s


memory and printing symbols from the memory.
o Input-output program are employed for writing programs for the
computer, for example.
o Programs for the Basic Computer to input and output one character:
non-interrupt based programs.
A.L.P. to input one character & output one character

Input Program Output Program


1 ORG 100 /Origin of program is HEX 100 1 ORG 100 /Origin of program is HEX 100
2 CIF, SKI /Check input flag 2 LDA CHR /Load character into AC
3 BUN CIF /Flag = 0, branch to check again 3 COF, SKO /Check output flag
4 INP /Flag = 1, input character 4 BUN COF /Flag = 0, branch to check again
5 OUT /Print character 5 OUT /Flag = 1, output character
6 STA CHR /Store character 6 HLT
7 HLT
7 CHR, HEX 0057 /Character is “W”
8 CHR, - /Store character here
8 END
9 END
Character Manipulation

 The symbolic program is stored in a section of the memory called the


buffer.

 A buffer is a set of consecutive memory locations that stores data


entered via the input device.

 Ex: store input characters in a buffer


Conti…

o Program to Store Input Characters in a Buffer


o Buffer Address is 500H

LDA ADS /AC500 IN2, HEX 0 /subroutine entry


STA PTR /PTR500 FST, SKI
LOP, BSA IN2 /AC2 character BUN FST
STA PTR I /(PTR)AC INP /input first char
ISZ PTR /PTRPTR+1 OUT
BUN LOP /PCLOP BSA SH4 /shift left four
HLT BSA SH4 /shift left four
ADS, HEX 500 SCD, SKI
PTR, HEX 0 BUN SCD
INP /input second char
OUT
BUN IN2 I
Conti…
• First char: A

• First char ASCII (Hex): 41

• Binary : 0000 0000 0100 0001 (0041)

• First Shift: 0000 0100 0001 0000 (0410)

• Second Shift: 0100 0001 0000 0000 (4100)

• Second char: N

• Second char ASCII (Hex): 4E

• Binary: 0000 0000 0100 1110 (004E)

• Final : 0100 0001 0100 1110 (414E) (AN)


Table lookup

o A two pass assembler performs the table lookup in the


second pass.

o This is an operation that searches a table to find out if it


contains a given symbol.

o The search may be done by comparing the given symbol with


each of the symbols stored in the table.
Conti….

o The search terminates when a match occurs or if none of the


symbols match.
o The comparison is done by forming the 2’s complement of a
word and arithmetically adding it to the second word.
o If the result is zero, the two words are equal and a match
occurs. Else, the words are not the same.
Compare two words

LDA WD1 / Load first word


CMA
INC / Form 2’s complement
ADD WD2 / Add second word
SZA / Skip if AC is zero
BUN UEQ / Branch to “unequal” routine
BUN EQL / Branch to “equal” routine
WD1, ---
WD2, ---
Program Interrupt

o Programmed IO
o Most of CPU time is spent at checking IF or OF
o Loss of CPU time
o Easy to Implement
o Interrupt
o CPU request I/O to device
o CPU goes to do Other jobs
o Device Interrupt CPU when I/O operation is Done
o CPU performs Interrupt Service Routine
Interrupt Service Routine

o Interrupt Occurs
o for Interrupt cycle
o IEN0, (0)PC, PC1
o Save Contents of processor registers
o Basic Computer has AC and E registers
o Check which Flag is Set
o IF(FGI) and OF(FGO) Flag
o Priority of Interrupt: Which is serviced first?
o Service the device whose flag is set
o Restore Contents of processor registers
o Turn the Interrupt facility On
o IEN1, for Next interrupt
o Return to the running program
0 ZRO, HEX 0

Ex: ISR 1

BUN SRV

200 SRV, STA SAC


CIR
STA SE
SKI
BUN NXT
INP
100 CLA OUT
101 ION STA PT1 I
102 LDA X Interrupt Occurs ISZ PT1
103 ADD Y NXT, SKO
104 STA Z BUN EXT
… LDA PT2 I
OUT
ISZ PT2
EXT, LDA SE
CIL
LDA SAC
ION
BUN ZRO I
SAC,
SE,
PT1,
PT2,
76

You might also like