---
# System prepended metadata

title: 系統程式第二章
tags: [學業共筆]

---

### Basic Assembler Functions

**Assembler directives(不會占memory空間)**
* START
    - 幫程式命名並且標註一個起始位置
* END
    - 標註程式的結束位置且記住程式的第一個instruction
* BASE

```antlr4=
Line 	Loc 	Source statement 		Object code 

5 	1000 	COPY 	START	 1000 
10 	1000 	FIRST 	STL	 RETADR 	141033 
255 			END 	 FIRST 
```



## SIC Assembler

**Assembler在作業系統中的位置**

![image](https://hackmd.io/_uploads/Bk2PyHENT.png)
> 圖片來源: https://www.spreered.com/compiler_for_dummies/

Assembler會將組合語言轉成Object code(機器語言)

<h3 style="color:orange;">Forward refrence問題(必考)</h3>

問題:
* 在source還沒宣告前，無法產生Object code

解決方式:
* 把程式先看一遍，紀錄所有source的位置(two passes)

## Object code

**生成流程**

1. 透過Hash function建立Hashtable
2. PASS1 建立symbol table,生成一個中介檔(intermidiate file)
3. PASS2 透過intermidiate file生成object code


![](https://hackmd.io/_uploads/rkmax5vM6.png)

**n, i bit**

如果變數前面有`@`或`#`:
* `@`: indirect addressing mode
    - n = 1, i = 0
* `#`: imeediate addressing mode
    - n = 0, i = 1
* 如果沒有上述符號
    - n = 1, i = 1

**x bit**

如果變數含有`,`，為indexing mode
* x = 1

沒有的話則為Direct mode
* x = 0

**b, p bit**

預設使用**program-counter relative**:
* disp = TA - (PC)
* b = 0, p = 1

如果上述方法不行則用**base relative**:
* disp = TA - (B)
* b = 1, p = 0

如果上述方法都不行則:
* b = 0, p = 0

![](https://hackmd.io/_uploads/HJ_qQ9uMT.png)

**e bit**

如果指令前面有`+`，為**Format 4**
* e = 1

沒有的話則為
* e = 0

![image](https://hackmd.io/_uploads/HJ751vXNp.png)

## Figure 2.10 Program
```antlr4=
Line 	Loc 	   Source statement	 	Object code 	 

5 	0000 	COPY 	START 	0 		 
10 	0000 	FIRST 	STL 	RETADR 		17202D 	 
13 	0003 		LDB    #LENGTH 	69202D 	 
14 			BASE 	LENGTH 		 
15 	0006 	CLOOP  +JSUB 	RDREC 		4B101036 	 
20 	OOOA 		LDA 	LENGTH 		032026 	 
25 	OOOD 		COMP   #0 		290000 	 
30 	0010 		JEQ 	ENDFIL 		332007 	 
35 	0013 	       +JSUB 	WRREC 		4B10105D 	 
40 	0017 		J 	CLOOP 		3F2FEC 	 
45 	00lA 	ENDFIL 	LDA    =C'EOF' 	032010 	 
50 	001D 		STA 	BUFFER 		0F2016 	 
55 	0020 		LDA    #3 		010003 	 
60 	0023 		STA 	LENGTH 		0F200D 	 
65 	0026 		+JSUB 	WRREC 		4B10l05D 	 
70 	002A 		J      @RETADR 	3E2003 	 
93 			LTORG 			 
	002D 	*      =C'EOF' 		454F46 	 
95 	0030 	RETADR 	RESW 	1 		 
100 	0033 	LENGTH 	RESW 	1 			 
105 	0036 	BUFFER 	RESB 	4096 			 
106 	1036 	BUFEND 	EQU 	* 			 
107 	1000 	MAXLEN 	EQU 	BUFEND-BUFFER 			 
110 		.				 
115 		.	SUBROUTINE TO READ RECORD INTO BUFFER 	 
120 		.					 
125 	1036 	RDREC 	CLEAR 	X 		B410 	 
130 	1038 		CLEAR 	A 		B400 	 
132 	103A 		CLEAR 	S 		B440 	 
133 	103C 	       +LDT    #MAXLEN 	75101000 	 
135 	1040 	RLOOP 	TD 	INPUT 		E32019 	 
140 	1043 		JEQ 	RLOOP 		332FFA 	 
145 	1046 		RD 	INPUT 		DB2013 	 
150 	1049 		COMPR 	A,S 		A004 	 
155 	104B 		JEQ 	EXIT 		332008 	 
160 	104E 		STCH 	BUFFER,X 	57C003 	 
165 	1051 		TIXR 	T 		B850 	 
170 	1053 		JLT 	RLOOP 		3B2FEA 	 
175 	1056 	EXIT 	STX 	LENGTH 		134000 	 
180 	1059 			RSUB 		4F0000 	 
185 	105C 	INPUT 	BYTE 	X'F1' 		F1 	 
195 		.					 
200 		.		SUBROUTINE TO WRITE RECORD FROM BUFFER 	 
205 		.					 
210 	105D 	WRREC 	CLEAR 	X 		B410 	 
212 	105F 		LDT 	LENGTH 		774000 	 
215 	1062 	WLOOP 	TD     =X'05' 		E32011 	 
220 	106S 		JEQ 	WLOOP 		332FFA 	 
22S 	1068 		LDCH 	BUFFER, X 	53C003 	 
230 	106B 		WD     =X'OS' 		DF2008 	 
235 	106E 		TIXR 	T 		B850 	 
240 	1070 		JLT 	WLOOP 		3B2FEF 	 
245  	1073  	 	RSUB 		 	4F0000
255			END  	FIRST
 	1076	*      =X'05'			05
```

## Program Relocation

當程式有記憶體存取或者使用direct addresses會需要進行Relocation
* SIC/XE Format 4
* SIC

**Object program Modification record**
```an=
Line 	Loc 	   Source statement	 	Object code 	 

5 	0000 	COPY 	START 	0 	

15 	0006 	CLOOP  +JSUB 	RDREC 		4B101036 
Modification record: M00000705
```
* bit 1: M
* bit 2-7: 需要修改的位址(相對於起點)
* bit 8-9: 需要修改的範圍(單位為half-bytes)

## Literals
系統自動分配了一個記憶體空間並存放常數的值，並且指令中指向該常數記憶體位址
* 使用`=`開頭
* 把Literals集中在Literal pools，變數名稱為`*`
* 如果使用relative addressing mode，也會需要 Modification record

<a style="color:orange">**LTORG 必考**</a>
* 告訴系統將Literals宣告在底下
```an=
Line 	Loc 	   Source statement	 	Object code 	 

45 	00lA 	ENDFIL 	LDA    =C'EOF' 	032010 	 
        .
        .
93 			LTORG
	002D 	*      =C'EOF' 		454F46 	 		 
```


**LITTAB**

我們需要一個新的資料結構來儲存Literals型態，概念類似專門給Literals的symbol table
* Literal name(常數本身)
* 常數的值以及長度
* 記憶體位址

LITTAB會在Pass1時被辨識
在Pass2時，將變數的記憶體位置找出來

<h3 style="color:orange">Literals vs immediate operand 必考</h3>

**immediate operand**
* immediate addressing, the operand value is assembled as part of the machine instruction. 

**Literals**
* With a literal, the assembler generates the specified value as a constant at some other memory location. 


## Symbol-Defining Statements

<h3 style="color:orange">EQU 必考</h3>

類似於C的`#define`，用於定義字串和數值的關係

EQU也可用於給予暫存器(Register)的值

**優點**
* 常用於建立符號型的名稱，**增加程式可讀性**
* 不佔用記憶體空間
* 不用memory access

**ORG** 
* 可以更動location counter的值

## Expressions

<h3 style="color:orange">absolute term vs relative terms 必考</h3>

在symbol table裡會有flag來標示該值為absolute或者relative term

**absolute term 必考**
* 常數(Constant)

**relative terms**

* Labels on instructions and data areas, and references to the location counter value, are relative terms.

![image](https://hackmd.io/_uploads/H16wXvNVa.png)

```antlr4=
Line 	Loc 	   Source statement	 	Object code 	 
95 	0030 	RETADR 	RESW 	1 		 
100 	0033 	LENGTH 	RESW 	1 			 
105 	0036 	BUFFER 	RESB 	4096 			 
106 	1036 	BUFEND 	EQU 	* 			 
107 	1000 	MAXLEN 	EQU 	BUFEND-BUFFER
```