DV & IIS Lab.
Assembly Programming
Department of Cyber Security
Ajou University
Manpyo Hong
Instruction set
Memory reference Instructions
Symbol Hexa code Description
I=0 I=1 Address
AND 0 8 AD AND Memory word to AC
ADD 1 9 AD ADD Memory word to AC
Register reference Instructions
LDA 2 A AD LOAD AC from Memory
STA 3 B AD STORE AC into Memory Hexa code
Symbol Description
BUN 4 C AD BRANCH unconditionally
CLA 7800 Clear AC
BSA 5 D AD BRANCH and save return address
CLE 7400 Clear E
ISZ 6 E AD Increment and skip of zero1
CMA 7200 Complement AC
CME 7100 Complement E
Input output Instructions CIR 7080 Circular right E and AC
CIL 7040 Circular left E and AC
Symbol Hexa code Description INC 7020 Increment AC
INP F800 Input INPR to AC SPA 7010 Skip if AC is positive
OUT F400 Output AC to OUTR SNA 7008 Skip if AC is negative
SKI F200 Skip if FGI is set SZA 7004 Skip if AC is zero
SKO F100 Skip if FGO is set SZE 7002 Skip if E is zero
ION F080 Enable interrupt HLT 7001 Halt computer
IOF F040 Disable interrupt
DV & IIS Lab. 2
Programming Language
location Machine code Hexa code Symbolic Comments
000 0010 0000 0000 0100 2004 LDA 004 Load first operand into AC
001 0001 0000 0000 0101 1005 ADD 005 Add second operand to AC
010 0011 0000 0000 0110 3006 STA 006 Store sum in location 006
011 0111 0000 0000 0001 7001 HLT Halt computer
100 0000 0000 0101 0011 0053 0053 First operand
101 1111 1111 1110 1001 FFE9 FFE9 Second operand
110 0000 0000 0000 0000 0000 0000 Store sum here
Assembly code
ORG. 0 /Origin of program is location 0 FORTRAN code
LDA. A /Load operand from location A
INTEGER A, B, C
ADD B /Add operand from location B
DATA A=83, B=-23
STA. C /Store sum in location C C=A+B
HLT /Halt computer END
A, DEC 83 /Decimal value 83
B, DEC-23 /Decimal value -23
C, DEC 0 /Sum stored in location C
END /End of symbolic program
DV & IIS Lab. 3
Assembly Language
Symbolic Pseudo instruction
ORG N Hexadecimal number N is the memory location for the instruction or operand listed in the following line
END Denote the end of symbolic program
DEC N Signed decimal number N to be converted binary
HEX N Hexadecimal number N to be converted binary
Assembly code
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
MIN, DEC 83 /Minuend
SUB, DEC-23 /Subtrahend
DIF HEX 0 /Difference stored here
END /End of symbolic program
DV & IIS Lab. 4
Assembler
First pass
• Two pass algorithm
• pass1 : create symbol table LC <- 0
• pass2 : generate machine code
Scan next line of code Set LC
Assembly code yes
ORG 100 /Origin of program is location 100
label no ORG
LDA SUB /Load subtrahend to AC
CMA /Complement AC yes no
INC /Increment AC
Store symbol in no yes
ADD MIN /Add minuend to AC
address-symbol END
STA DIF /Store difference table together
HLT with value of LC
MIN, DEC 83 /Minuend
SUB, DEC -23 /Subtrahend Go to
Increment LC second
DIF HEX 0 /Difference stored here pass
END /End of symbolic program
DV & IIS Lab. 5
Assembler - pass 2
Second pass
Scan next line of code Set LC done
yes yes
yes no
pseudo ORG END
address Machine code Symbol code no no
15 12-14 0-11 ORG 100 yes Convert
no
MRI
100 0 010 0001 0000 0111 LDA SUB value
101 0 111 0010 0000 0000 CMA from
Set operation Valid Hex or
102 0 111 0000 0010 0000 INC Instruction? DEC to
code
103 0 001 0001 0000 0110 ADD MIN binary
yes
104 0 011 0001 0000 1000 STA DIF
Search symbol Store binary
105 0 111 0000 0000 0001 HLT table for in location
106 0 0 0000 0101 0011 DEC 83 address field given by LC
Error
107 1 111 1111 1110 1001 DEC-23
yes no in line
108 0 000 0000 0000 0000 HEX 0 I of code
END
1st bit <- 1 1st bit <- 0
Increment LC
DV & IIS Lab. 6
Program Loop
line Assembly code line Assembly code
1 ORG 100 /Origin of program is HEX 100 18 ORG 150 /Origin of operand is HEX 150
2 LAD ADS /load first address of operands 19 DEC 75 /load first address of operands
3 STR PTR /store in pointer
4 LDA NBR /load minus 100
5 STR CTR /store in counter
6 CLA /clear AC 118 DEC 23 /last operand
7 LOP, ADD PTR I /add an operand to AC 119 END /End of symbolic program
8 ISZ PTR /increment pointer
9 ISZ CTR /increment counter
10 BUN LOP /repeat loop again
11 STA SUM /Store sum
12 HLT /halt
13 ADS, HEX 150 /first address of operand
14 PTR, HEX 0 /the location reserved for a pointer
15 NBR, DEC -100 /constant to initialized counter
16 CTR, HEX 0 /the location reserved for a counter
17 SUM, HEX 0 /sum is stored here
DV & IIS Lab. 7
Multiplication
LOP, CLE /Clear E
LAD Y /load multiplier
CIR /Transfer multiplier bit to E
STA Y /store shifted multiplier
SZE /check if bit is zero
BUN ONE /bit is one, go to ONE
BUN ZRO /bit is zero, go to ZRO
ONE, LDA X /load multiplicand
ADD P /add to partial product
STA P /Store partial product
CLE /clear E
ZRO, LDA X /load multiplicand
CIL /shift left
STA X /store shifted multiplicand
ISZ CTR /increment counter
BUN LOP /counter not zero; repeat loop
HLT /counter is zero; halt
CTR, DEC -8 /counter value
X, HEX 000F /multiplicand stored here
Y, HEX 0008 /multiplier stored here
P, HEX 0 /product formed here
END
DV & IIS Lab. 8
Subroutine
ORG 100 /main program
100 LDA X /load X
101 BSA SH4 /branch to subroutine X 0001 0010 0011 0100
102 STA X /store shifted number
103 LDA Y /load Y
104 BSA SH4 /branch to subroutine again Y 0100 0011 0010 0001
105 STA Y /store shifted number
106 HLT
107 X, HEX 1234 SH4
108 Y, HEX 4321
109 SH4, HEX 0 /subroutine to shift left 4 times
10A CIL /circulate left once
MSK 1111 1111 1111 0000
10B CIL
10C CIL
10D CIL /circulate left 4th time
10E AND MSK /set AC(12-15) to zero
10F BUN SH4 I /return to main program
110 MSK, HEX FFF0 /mask operand
END
DV & IIS Lab. 9
Parameter passing
ORG 200 /main program
200 LDA X /load X
▪ Parameter passing
201 BSA OR /branch to subroutine OR
202 HEX 3AF6 /store shifted number
• single
203 STAY /second operand stored here
• multiple
204 HLT
205 X, HEX 7B95 /first operand
206 Y, HEX 0 /result stored here
207 OR, HEX 0 /subroutine OR
208 CMA /complement first operand
What function is this program?
209 STA TMP /store in temporary location
20A LDA OR I /load second operand
20B CMA /complement second operand
20C AND TMP /AND complemented first operand
20D CMA /complemented again to get OR
20E ISZ OR /increment return address
20F BUN OR I /return to main program
210 TMP, HEX 0
END
DV & IIS Lab. 10
Block transfer
BSA MVE /branch to subroutine
HEX 100 /first address of source data
HEX 200 /second address of destination data
PT1, HEX 0
DEC -16 /number of items to move
PT2, HEX 0
HLT
CTR HEX 0
MVE, HEX 0 /subroutine MVE
LDA MVE I /bring address of source
STA PT1 /store in first pointer
ISZ MVE /increment return address
ORG 100
LDA MVE I /bring address of destination
100 38 /first element of source array
STA PT2 /store in second pointer
:::
ISZ MVE /increment return address
10F 9D /last element of source array
LDA MVE I / bring number of items
STA CTR /store in counter
ORG 200
ISZ MVE /increment return address
200 0 /first element of destination array
LOP, LDA PT1 I /load source item
:::
STA PT2 I //store in destination
20F 0 /last element of destination array
ISZ PT1 /increment source pointer
ISZ PT2 /increment destination pointer
ISZ CTR /increment counter
BUN LOP /repeat 16 items
BUN MVE I /return to main program
DV & IIS Lab. 11
I/O programming - packing
CIF, SKI /check input flag IN2, HEX 0
BUN CIF /flag=0, branch to check again FST, SKI
INP /flag=1, input character BUN FST
OUT /print character INP /input first character
STA CHR /store character OUT
HLT BSA SH4 /shift left 4 times
CHR, HEX 0 BSA SH4 /shift left 4 times again
SCD, SKI
BUN SCD
LDA CTR /load character into AC INP /input second character
COF, SKO /check output flag OUT
BUN COF /flag=0, branch to check again BUN IN2 I
OUT /flag=1, output character
HLT
CTR HEX 0057 /character is ‘W’
DV & IIS Lab. 12
I/O programming - buffering
LDA ADS /load first address of buffer
STA PTR /initialize pointer
LOP, BSA IN2 /go to subroutine IN2
STA PTR I /store double character word in buffer • SPOOL in OS
ISZ PTR /increment pointer
BUN LOP /branch to input more character
HLT
ADS, HEX 500 /first address of buffer
PTR HEX 0 /location for pointer
LDA WD1 /load first word
CMA
INC /2’s complement
ADD WD2 /add second word
SZA /skip if Ac is zero
BUN UEQ /branch to “unequal” routine
BUN EQL /ranch to “equal” routine
WD1, -
WD2, -
DV & IIS Lab. 13
Programmed Interrupt Service
0 ZRO HEX 0 /save area NXT SKO /check output flag
1 BUN SRV /branch to service routine BUN EXT /flag=0, exit
OUT /print character
100 CLA /running program LDA PT2 I /output character
101 ION /turn on interrupt facility ISZ PT2
102 LDA X EXT, LDA SE /restore value AC(0)
103 ADD Y /interrupt occurs here CIL /shift it to E
104 STA Z /program returns here after interrupt LDA SAC /restore content of AC
::: ION /turn interrupt on
ORG 200 BUN ZRO I /return to running program
200 SRV, STA SAC /save AC SAC HEX 0 /save area of AC
201 CIR /move E to AC(0) SE, HEX 0 /Save area of E
STA SE /store E PT1, HEX 0FF0 /pointer of input buffer
SKI /check input flag PT2 HEX 0FE0 /pointer of output buffer
BUN NXT /flag=0,check next flag
INP /flag=1, input character Prologue
OUT /printer character
STA PT1 I /save it in input buffer Epilogue
ISZ PT1 /increment input pointer
DV & IIS Lab. 14