04 Assembly Programming
04 Assembly Programming
04 Assembly Programming
Introduction Machine Language Assembly Language Assembler Program Loops Programming Arithmetic and Logic Operations Subroutines Input-Output Programming
Introduction
INTRODUCTION
Those concerned with computer architecture should have a knowledge of both hardware and software because the two branches influence each other. Instruction Set of the Basic Computer
Symbol AND ADD LDA STA BUN BSA ISZ CLA CLE CMA CME CIR CIL INC SPA SNA SZA SZE HLT INP OUT SKI SKO ION IOF Hexa code Description 0 or 8 AND M to AC 1 or 9 Add M to AC, carry to E 2 or A Load AC from M 3 or B Store AC in M 4 or C Branch unconditionally to m 5 or D Save return address in m and branch to m+1 6 or E Increment M and skip if zero 7800 Clear AC 7400 Clear E 7200 Complement AC 7100 Complement E 7080 Circulate right E and AC 7040 Circulate left E and AC 7020 Increment AC, carry to E 7010 Skip if AC is positive 7008 Skip if AC is negative 7004 Skip if AC is zero 7002 Skip if E is zero 7001 Halt computer F800 Input information and clear flag F400 Output information and clear flag F200 Skip if input flag is on F100 Skip if output flag is on F080 Turn interrupt on F040 Turn interrupt off m: effective address M: memory word (operand) found at m
Machine Language
MACHINE LANGUAGE
Program A list of instructions or statements for directing the computer to perform a required data processing task Various types of programming languages - Hierarchy of programming languages Machine-language - Binary code - Octal or hexadecimal code Assembly-language - Symbolic code High-level language (Assembler)
(Compiler)
Machine Language
Hexa program
Location 000 001 002 003 004 005 006 Instruction 2004 1005 3006 7001 0053 FFE9 0000
Assembly-Language Program
ORG LDA ADD STA HLT DEC DEC DEC END 0 A B C 83 -23 0 /Origin of program is location 0 /Load operand from location A /Add operand from location B /Store sum in location C /Halt computer /Decimal operand /Decimal operand /Sum stored in location C /End of symbolic program
A, B, C,
Fortran Program
INTEGER A, B, C DATA A,83 / B,-23 C=A+B END
Assembly Language
ASSEMBLY LANGUAGE
Syntax of the BC 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 or a pseudo instruction - May specify one of * Memory reference instr. (MRI) MRI consists of two or three symbols separated by spaces. ADD OPR (direct address MRI) ADD PTR I (indirect address MRI) * Register reference or input-output instr. Non-MRI does not have an address part * Pseudo instr. with or without an operand Symbolic address used in the instruction field must be defined somewhere as a label Comment field - May be empty or may include a comment
Assembly Language
PSEUDO-INSTRUCTIONS
ORG N Hexadecimal number N is the memory loc. 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 Example: Assembly language program to subtract two numbers
ORG 100 LDA SUB CMA INC ADD MIN STA DIF HLT DEC 83 DEC -23 HEX 0 END / Origin of program is location 100 / Load subtrahend to AC / Complement AC / Increment AC / Add minuend to AC / Store difference / Halt computer / Minuend / Subtrahend / Difference stored here / End of symbolic program
Assembly Language
TRANSLATION TO BINARY
Hexadecimal Code Location Content 100 101 102 103 104 105 106 107 108 2107 7200 7020 1106 3108 7001 0053 FFE9 0000
Symbolic Program ORG 100 LDA SUB CMA INC ADD MIN STA DIF HLT DEC 83 DEC -23 HEX 0 END
Assembler
ASSEMBLER
Assembler
- FIRST PASS -
Source Program - Symbolic Assembly Language Program Object Program - Binary Machine Language Program
First pass
First pass
LC := 0
Scan next line of code Set LC yes Label yes Store symbol in addresssymbol table together with value of LC Increment LC END no yes no ORG no
Go to second pass
Assembler
ASSEMBLER
- SECOND PASS -
Second Pass Machine instructions are translated by means of table-lookup procedures; (1. Pseudo-Instruction Table, 2. MRI Table, 3. Non-MRI Table 4. Address Symbol Table)
Second pass
LC <- 0 Scan next line of code
Done
Set LC yes yes no END
Pseudo instr. no yes Get operation code and set bits 2-4 Search addresssymbol table for binary equivalent of symbol address and set bits 5-16 yes I no MRI
yes
ORG
no
no DEC or HEX Convert operand to binary no and store in location given by LC
10
Program Loops
PROGRAM LOOPS
Loop: A sequence of instructions that are executed many times, each with a different set of data Fortran program to add 100 numbers: DIMENSION A(100)
3 INTEGER SUM, A SUM = 0 DO 3 J = 1, 100 SUM = SUM + A(J)
LOP,
11
12
FLOWCHART OF A PROGRAM - Multiplication CTR - 8 P0 E0 X holds the multiplicand Y holds the multiplier P holds the product Example with four significant digits X = 0000 1111 Y = 0000 1011 0000 1111 0001 1110 0000 0000 0111 1000 1010 0101 E0 AC X cil EAC cil X AC CTR CTR + 1 0 =0 P 0000 0000 0000 1111 0010 1101 0010 1101 1010 0101
AC Y
cir EAC Y AC =0 =1 PP+X
CTR
Stop
13
ASSEMBLY LANGUAGE PROGRAM - Multiplication ORG 100 CLE LDA Y CIR STA Y SZE BUN ONE BUN ZRO LDA X ADD P STA P CLE LDA X CIL STA X ISZ CTR BUN LOP HLT DEC -8 HEX 000F HEX 000B HEX 0 END
LOP,
ONE,
ZRO,
CTR, X, Y, P,
/ Clear E / Load multiplier / Transfer multiplier bit to E / Store shifted multiplier / Check if bit is zero / Bit is one; goto ONE / Bit is zero; goto ZRO / Load multiplicand / Add to partial product / Store partial product / Clear E / Load multiplicand / Shift left / Store shifted multiplicand / Increment counter / Counter not zero; repeat loop / Counter is zero; halt / This location serves as a counter / Multiplicand stored here / Multiplier stored here / Product formed here
14
/ Load A low / Add B low, carry in E / Store in C low / Clear AC / Circulate to bring carry into AC(16) / Add A high and carry / Add B high / Store in C high
15
16
Subroutines
SUBROUTINES
Subroutine - A set of common instructions that can be used in a program many times. - Subroutine linkage : a procedure for branching to a subroutine and returning to the main program Example
Loc. 100 101 102 103 104 105 106 107 108 109 10A 10B 10C 10D 10E 10F 110 ORG 100 LDA X BSA SH4 STA X LDA Y BSA SH4 STA Y HLT HEX 1234 HEX 4321 HEX CIL CIL CIL CIL AND BUN HEX END 0 / Main program / Load X / Branch to subroutine / Store shifted number / Load Y / Branch to subroutine again / Store shifted number
X, Y, SH4,
/ Subroutine to shift left 4 times / Store return address here / Circulate left once / Circulate left fourth time / Set AC(13-16) to zero / Return to main program / Mask operand
MSK,
17
Subroutines
X, Y, OR,
TMP,
18
Subroutines
SUBROUTINE - Moving a Block of Data BSA MVE HEX 100 HEX 200 DEC -16 HLT HEX 0 LDA MVE I STA PT1 ISZ MVE LDA MVE I STA PT2 ISZ MVE LDA MVE I STA CTR ISZ MVE LDA PT1 I STA PT2 I ISZ PT1 ISZ PT2 ISZ CTR BUN LOP BUN MVE I ---/ Main program / Branch to subroutine / 1st address of source data / 1st address of destination data / Number of items to move / Subroutine MVE / Bring address of source / Store in 1st pointer / Increment return address / Bring address of destination / Store in 2nd pointer / Increment return address / Bring number of items / Store in counter / Increment return address / Load source item / Store in destination / Increment source pointer / Increment destination pointer / Increment counter / Repeat 16 times / Return to main program
MVE,
LOP,
Fortran subroutine
SUBROUTINE MVE (SOURCE, DEST, N) DIMENSION SOURCE(N), DEST(N) DO 20 I = 1, N 20 DEST(I) = SOURCE(I) RETURN END
19
CHR,
Program to Output a Character COF, LDA CHR SKO BUN COF OUT HLT HEX 0057 / Load character into AC / Check output flag / Flag=0, branch to check again / Flag=1, output character / Character is "W"
CHR,
20
CHARACTER MANIPULATION
Subroutine to Input 2 Characters and pack into a word
IN2, FST,
-SKI BUN INP OUT BSA BSA SCD, SKI BUN INP OUT BUN
/ Subroutine entry FST / Input 1st character SH4 SH4 SCD / Input 2nd character IN2 I / Return / Logical Shift left 4 bits / 4 more bits
21
PROGRAM INTERRUPT
Tasks of Interrupt Service Routine
- Save the Status of CPU Contents of processor registers and Flags - Identify the source of Interrupt Check which flag is set - Service the device whose flag is set (Input Output Subroutine) - Restore contents of processor registers and flags - Turn the interrupt facility on - Return to the running program Load PC of the interrupted program
22
SRV,
NXT,
EXT,