16

Click here to load reader

Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Embed Size (px)

Citation preview

Page 1: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-1Chap. 6 Programming the Basic Computer

6-1 Introduction Translate user-oriented symbolic program(alphanumeric character set) into

binary programs recognized by the hardware 25 Instruction Set of the basic computer

Memory Reference Instruction Register Reference Instruction Input-output Instruction

Symbol Hex Code DescriptionAND 0xxx 8xxx And memory word to ACADD 1xxx 9xxx Add memory word to ACLDA 2xxx Axxx Load memory word to ACSTA 3xxx Bxxx Store content of AC in memoryBUN 4xxx Cxxx Branch unconditionallyBSA 5xxx Dxxx Branch and Save return addressISZ 6xxx Exxx Increment and skip if zeroCLA 7800 Clear ACCLE 7400 Clear ECMA 7200 Complement ACCME 7100 Comp le m e nt ECIR 7080 Circulate right AC and ECIL 7040 Circulate left AC and EINC 7020 Increment ACSPA 7010 Skip next instruction if AC positiveSNA 7008 Skip next instruction if AC negativeSZA 7004 Skip next instruction if AC zeroSZE 7002 Skip next instruction if E is 0HLT 7001 Halt computerINP F800 Input character to ACOUT F400 Output character from ACSKI F200 Skip on input flagSKO F100 Skip on output flagION F080 Interrup t O nIOF F040 Inter ru p t O ff

6-2 Machine Language Program Categories

1) Binary Code(Machine Language)» Program Memory 의 내용 : Tab. 6-2

2) Octal or Hexadecimal Code» Binary Code 와 동일 : Tab. 6-3

3) Symbolic Code : Tab. 6-4

» Assembly Language : Tab. 6-5 4) High Level Language

» C, Fortran,.. : Tab 6-6* Tab. 6-4 와 차이점 Pseudoinstruction, Label 사용 가능

Page 2: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-2

6-3 Assembly Language The rules for writing assembly language program

Documented and published in manuals(from the computer manufacturer)

Rules of the Assembly Language Each line of an assembly language program is arranged in three columns

» 1) Label field : empty or symbolic address

» 2) Instruction field : machine instruction or pseudoinstruction

» 3) Comment field : empty or comment Symbolic Address(Label field)

» One, two, or three, but not more than three alphanumeric characters

» The first character must be a letter; the next two may be letters or numerals

» A symbolic address is terminated by a comma(recognized as a label by the assembler) Instruction Field

» 1) A memory-reference instruction(MRI) Ex) ADD OPR(direct address MRI), ADD PTR I(indirect address MRI)

» 2) A register-reference or input-output instruction(non-MRI) Ex) CLA(register-reference), INP(input-output)

» 3) A pseudoinstruction with(ORG N) or without(END) an operand : Tab. 6-7 Pseudoinstruction 은 Machine Instruction 이 아니고 Assembler 에게 필요한 정보만 제공

Field

Label Instruction Comment

Page 3: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-3

Comment field » Comment filed must be preceded by a slash(recognized by assembler as comment)

An Example Program : Tab. 6-8 83 - ( - 23 ) = 83 + ( 2’s Complement of -23)

= 83 + 23 Translation to Binary : Tab. 6-9

Assembler = the translation of the symbolic(= assembly) program into binary Address Symbol Table = Hexadecimal address of symbolic address

» MIN = 106, SUB = 107, DIF = 108

Two Pass Assembler : in next Sec. 6-4

1) 1st scan pass : generate user defined address symbol table 2) 2nd scan pass : binary translation

6-4 The Assembler Source Program Object Code Binary Code

Ex) LDA SUB1) SUB = 107 2) 2107

교과서에서는 같은 의미로 사용됨

Assembler(Compiler)

Linker

asma96a51

c(cpp)forpas

obj

binexecomhex

Library 또는 외부 함수를 사용하여 Relocation

Page 4: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-4

Representation of Symbolic Program in Memory : Tab. 6-11 Line of Code : PL3, LDA SUB I (Carriage return)

» The assembler recognizes a CR code as the end of a line of code

LC 0

Scan next line of codeSet LC

Label O RG

Store symbol in address-symbol tabletogether withvalue of LC

Inc rement LC

EN D

Go tosecond

pass

First pass

yes

no

yes

no

yesno

yes

Two Pass Assembler 1) 1st pass : Generate user-

defined address symbol table» Flowchart of first pass :

Fig. 6-1» Address Symbol Table for

Program in Tab. 6-8 :

Tab. 6-12

Fig. 6-1 Flowchart for first pass of assembler

Page 5: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-5

2) 2nd pass : Binary translation» 다음의 4 개의 Table 을 참고하여

Instruction Format 에 의한 Binary Code 형성 (Pseudoinstruction Table, MRI Table, Non-MRI Table, Address Symbol Table)

» Flowchart of second pass :

Fig. 6-2» Binary Code translation 예제 :

Tab. 6-9 의 Content

» Error Diagnostics Check for possible errors in the

symbolic program Ex) Invalid Machine Code Error

Pseudoinstruction,MRI, non-MRI 에

속하지 않음

LC 0

Scan next line of code

Pseudo-instruction ORG END

Set LC

Done

M RI

G et operation code and set bits 2- 4

Validnon-M RI

instruction

Search address-symbol table forbinary equivalent

of symbolic addressand set bits 5- 16

I

Set firstbit to 1

Set firstbit to 0

Assembley all parts ofbinary instruc tion and

store in location given by LC

Store binaryequivalent ofinstruc tion in location

given by LC

Error in line of code

C onvertoperandto binaryand storein location

given by LC

Inc rement LC

Second pass

Yes

Yes

No

Yes

No

Yes

No

Yes

No

Yes No

No

DEC or HEX

I Opcode Address

1 2 3 4 5 6 …. … 15 16

Error

Fig. 6-2 Flowchart for second pass of assembler

Tab. 6-9 에서의 Contents 를 결정

이 경우는Hand Assemble

Page 6: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-6

6-5 Program Loops Program Loops

A sequence of instructions that are executed many times

Example of program loop Sum of 100 integer numbers

» Fortran

Symbolic Program : Tab 6-13» Address 150 부터 100 개의

Data 를 더하기

Line1 ORG 1002 LDA ADS / A = 1503 STA PTR / PTR = 1504 LDA NBR / A = - 1005 STA CTR / CTR = - 1006 CLA / A = 07 LOP, ADD PTR I / A + 75 (150 )번지의 내용8 ISZ PTR / 150 + 1 = 1519 ISZ CTR / - 100 + 1 = - 9910 BUN LOP / Loop until CTR = 0 11 STA SUM / Store A to SUM12 HLT13 ADS, HEX 15014 PTR, HEX 0 / 15015 NBR, DEC - 10016 CTR, HEX 0 / - 10017 SUM, HEX 0 / Result of Sum18 ORG 15019 DEC 75, , ,, , .

118 DEC 23119 END

DIMENSION A(100) INTEGER SUM, A SUM = 0 DO 3 J = 1, 1003 SUM = SUM + A(J)

Tab. 6-13 Symbolic Program to Add 100 numbers

Data

Page 7: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-7

6-6 Programming Arithmetic & Logic Operations Hardware implementation

Operations are implemented in a computer with one machine instruction Ex) ADD, SUB : 그러나 자리수가 늘어나면 Software subroutine 처리

Software implementation Operations are implemented by a set of instruction(Subroutine) Ex) MUL, DIV : 그러나 이와 같은 명령어를 갖는 CPU 도 있음

Multiplication Program Positive Number Multiplication

» X = multiplicand

Y = multiplier

P = Partial Product Sum

» Y 를 AC 에 저장한 후 E 로 Circular Right

E = 1 : P 에 1111 을 더함 E = 0 : 더하지 않음

X = 1 1 1 1

Y = 1 0 1 1

1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1

1 0 1 0 0 1 0 1

P = 0 0 0 0 0 0 0 0

+ 0 0 0 0 1 1 1 1

P = 0 0 0 0 1 1 1 1

+ 0 0 0 1 1 1 1 0

P = 0 0 1 0 1 1 0 1

+ 0 0 0 0 0 0 0 0

P = 0 0 1 0 1 1 0 1

+ 0 1 1 1 1 0 0 0

P = 1 0 1 0 0 1 0 1

AlgorithmFig. 6-3

Page 8: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-8

Line1 ORG 1002 LOP, CLE / A = 03 LDA Y / A = Y (000B)4 CIR / Circular Right to E5 STA Y / Store shifted Y6 SZE / Check if E = 07 BUN ONE / E = 18 BUN ZRO / E = 09 ONE, LDA X A = X (000F)10 ADD P / X = X + P 11 STA P / St or e A to P12 CLE / Clear E13 ZRO, LDA X / A = X14 CIL / A = 00011110 (00001111)15 STA X / St or e A to X16 ISZ CTR / CTR = - 7 = - 8 + 117 BUN LOP / Repeat until CTR = 018 HLT 19 CTR, DEC - 820 X, HEX 000F21 Y, HEX 000B22 P, HEX 023 END

E 0

Y AC

c ir EAC

AC Y

E

P P + X

E 0

AC X

X AC

C TR C TR + 1

c il EAC

CTR Stop

C TR - 8 P 0

=0 =1

≠ 0 = 0

Tab. 6-14 Program to Multiply Two Positive numbersFig. 6-3 flowchart for Multiplication Program

Page 9: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-9

Double Precision Addition : 32 bits 하위 AL + BL 먼저 수행하여 E 를 상위에 반영 (AH + BH + E)

Logic Operations Logic Operation 중에서 OR 명령이 없다 (Tab. 6-1)

» 추가 하려면 더 길은 Instruction Format 필요 해결 방법 : DeMorgan’s theorem

»

AH AL

31 16 15 0

BH BL

CH CL

Line1 LDA AL / A = AL2 ADD BL / A = AL + BL3 STA CL / Store A to CL4 CLA / A = 05 CIL / 0000 0000 0000 000?(?=E)6 ADD AH / A = 00(E=0) or 01(E=1)7 ADD BH / A = A + AH + BH8 STA CH / Store A to CH9 HLT 10 AL, DEC ? / Operand11 AH, DEC ? 12 BL, DEC ? 13 BH, DEC ? 14 CL, HEX 0 15 CH, HEX 0

+

BABABA

LDA A / Load ACMA / Complement A STA TMP / St or e t o T M PLDA B / Load BCMA / Complement BAND TMP / AND CMA / Complement

Page 10: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-10

Shift Operations Logical Shift : Zero must added to the extreme position

» Shift Right

» Shift Left

Arithmetic Shift Right» Positive ( + = 0 )

» Negative ( - = 1 )

CLECIR 0 0

E

0 0

ECLECIL

0 0

E

1 1

E

CLE / E= 0SPA / Skip if A= +, E= 0 CME / Toggle E(=1) if A= -CIR / Circulate A with E

6-7 Subroutines Subroutine

A set of common instruction that can be used in a program many times In basic computer, the link between the main program and a subroutine is the

BSA instruction(Branch and Save return Address) Subroutine example : Tab. 6-16

Page 11: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-11

Location ORG 100 / Main Program

100 LDA X / Load X101 BSA SH4 / Call SH4 with X102 STA X / Store result X103 LDA Y / Load Y104 BSA SH4 / Call SH4 with Y105 STA Y / Store result Y106 HLT 107 X, HEX 1234 / Result = 2340108 Y, HEX 4321 / Result = 3210

/ Subro u t in e 109 SH4, HEX 0 / Save Return Address10A CIL 10B CIL 10C CIL 10D CIL 10E AND MSK / Mask lower 4 bit10F BUN SH4 I / Indirect Return to main 110 MSK, HEX FFF0 / Mask pattern 110 END

SubroutineCALL hear

X = 102Y = 105

Tab. 6-16 Program to Demonstrate the use of Subroutines

Ex) 1234- CIL 4 회 = 2340- Mask : AND FFF0- 결과 = 2340

Page 12: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-12

Subroutine Parameters & Data Linkage Parameter(or Argument) Passing

» When a subroutine is called, the main program must transfer the data

2 가지 Parameter Passing 방법» 1) Data transfer through the

Accumulator Used for only single input and single

output parameter

» 2) Data transfer through the Memory 여러 개의 Operand 전달 가능 Operand are often placed in memory

locations following the CALL

2 개의 Parameter Passing 예 : Tab. 6-17

» First Operand and Result : Accumulator

» Second Operand : Inserted in location following the BSA

BSA 후에 2 개 Operand 예 : Tab. 6-18» BSA 후에 2 개의 Operand 사용» Block 전송 Source 와 Destination

Address 로 사용

Location ORG 200

200 LDA X / Load first operand X201 BSA OR / Call OR with X202 HEX 3AF6 / Second operand203 STA Y / Subroutine return here(Y=result)204 HLT 205 X, HEX 7B95 / First operand206 Y, HEX 0 / Result store here207 OR, HEX 0 / Return address = 202208 CMA / Complement X209 STA TMP / TMP = X 20A LDA OR I / A = 3AF6 (202 )번지의 내용20B CMA / Complement Second operand20C AND TMP / AND 20D CMA / Complement 전체20E ISZ OR / Return Address = 202 + 1 = 20320F BUN OR I / Return to main210 TMP, HEX 0

END

Call 후에 Return Address 를 이용

Tab. 6-17 Program to Demonstrate Parameter Linkage

* OR SubroutineFirst Operand : X = 7B95Second Operand : BSA 후 = 3AF6

Page 13: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-13

ORG 100 100 BSA MVE / Subroutine Call101 HEX 200 / Source Address102 HEX 300 / Destin a ti o n Address103 DEC - 16 / Number of data to move104 HLT 105 MVE, HEX 0 / Return address= 101106 LDA MVE I / A= 200 107 STA PT1 / PT1= 200108 ISZ MVE / Return address= 102109 LDA MVE I / A= 30010A STA PT2 / PT2= 30010B ISZ MVE / Return address= 10310C LDA MVE I / A= - 16 10D STA CTR / CTR= - 1610E ISZ MVE / Return address= 10410F LOP, LDA PT1 I / A= Address 200 의 내용110 STA PT2 I / Address 300 에 저장111 ISZ PT1 / PT1= 201112 ISZ PT2 / PT2= 301113 ISZ CTR / CTR= - 15 if 0 skip114 BUN LOP / Loop until CTR= 0115 BUN MVE I / 104 Retur n로 = HLT116 PT1, HEX ? / Source 117 PT2, HEX ? / Destination118 CTR, DEC ? / Counter

Tab. 6-18 Subroutine to Move a Block of Data

2 개의Operand

* Block Move Subroutine메모리 200 번지 부터

16 개의 데이터를 메모리300 번지로 이동

Page 14: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-14

6-8 Input-Output Programming One-character I/O

Programmed I/O 방식

Two-character I/O Two character Packing

(a) Input a characterCIF, SKI / Check FGI = 1 ? BUN CIF / Go to CIF if FGI= 0

INP / Input character (FGI = 1)OUT / Echo BackSTA CHR / Store characterHLT

CHR, ? / Store character here

LDA CHR / Load output characterCOF, SKO / Check FGO = 1 ? BUN COF / Go to COF if FGO= 0 OUT / Output character (FGO = 1) HLT CHR, HEX 0057 / Output character = "W"

(b) Output a character

IN2, HEX ? / Save return addressFST, SKI / Check if FGI= 1 ?

BUN FST / Loop (FGI = 0)INP / Input first characterOUT / Echo backBSA SH4 / Shift left 4 bit

BSA SH4 / Again(total 8 bit shift)SCD, SKI BUN SCD INP / Input second character OUT / Echo back BUN IN2 I / Return

1st Char

15 8 7 0

1st Char

1st Char 2nd Char

Tab. 6-19 Program to input and output One character Tab. 6-20 Subroutine to input and pack Two character

Page 15: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-15

Store Input Character in Buffer Compare Two Word

Tab. 6-21 Program to store input character in buffer Tab. 6-22 Program to compare Two word

LDA ADS / Load buffer address A= 500 STA PTR / PTR= 500LOP, BSA IN2 / Get a character (Tab. 6- 20)

STA PTR I / 500 character 번지에 저장ISZ PTR / PTR= 501BUN LOP / Endless Loop

HLT ADS, HEX 500 / Buffer address PTR, HEX 0 / Pointer

LDA WD1 / Load first word A= WD1 CMA / Make 2's complement INC

ADD WD2 / WD2 - WD1SZA / Skip if A=0 (Equal)BUN UEQ / Unequal

BUN EQL / EqualWD1, HEX ? / first wordWD2, HEX ? / second wor d

Page 16: Computer System Architecture Dept. of Info. Of Computer Chap. 6 Programming the Basic Computer 6-1 Chap. 6 Programming the Basic Computer n 6-1 Introduction

Computer System Architecture Dept. of Info. Of ComputerChap. 6 Programming the Basic ComputerChap. 6 Programming the Basic Computer

6-16

Interrupt Program Interrupt Condition

» Interrupt F/F R = 1

when IEN = 1 and [FGI or FGO = 1]

» Save return address at 0000

» Jump to 0001 (Interrupt Start) Interrupt Service Routine(ISR)

» 1) Save Register (AC, E)

» 2) Check Input or Output Flag

» 3) Input or Output Service Routine

» 4) Restore Register (AC, E)

» 5) Interrupt Enable (ION)

» 6) Return to the running program

Modified Fetch Cycle 과 Reset 시에 IEN = 0 이 된다

(p. 158, Fig. 5-15)

LocationORG 0

0 ZRO, HEX ? / Save I nterrupt Return Address1 BUN SRV / J ump to I SR

ORG 100 / Mai n program 100 CLA 101 I ON / Turn on I nterrupt(I EN= 1)102 LDA X 103 ADD Y / I nterrupt occurs here104 STA Z / Return Address(104) ORG 200

200 SRV, STA SAC / Save A to SAC 201 CI R / Move A i nto A(15) 202 STA SE / Sav e E t o S E 203 SKI / Check i f FGI = 1? 204 BUN NXT / No, FGI = 0, Check FGO 205 I NP / Yes, FGI = 1, Character I nput206 OUT / Echo back207 STA PT1 I / Store i n i nput buff er(PT1)208 I SZ PT1 / PT1 + 1

NXT, SKO / Check i f FGO= 1? BUN EXT / No, FGO= 0, Exi t LDA PT2 I / Yes, FGO= 1, Get output characterOUT / Character outputI SZ PT2 / PT2 + 1

EXT, LDA SECI L / Restore ELDA SAC / Restore AI ON / I nte rr u p t O NBUN ZRO I / Return to runni ng program(104)

SAC, HEX ?SE, HEX ?PT1, HEX 300 / I nput Buff er AddressPT2, HEX 400 / Output Buff er Address

InterruptHere