---
# System prepended metadata

title: 微處理機期末考練習題

---

# 微處理機期末考練習題  
### 第一題  
Write a program segment to convert an unknown number of packed BCD numbers begin at 1200:0120, until an FF is met, into ASCII and store at 3300:2300.  

```=
code segment
           assume cs:code
    start: mov    ax, 1200h
           mov    ds, ax
           mov    si, 120h
           mov    ax, 3300h
           mov    es, ax
           mov    di, 2300h
           mov    ax, 0
        
    label1:mov    al, [si]
           cmp    al, 0ffh 
           jz     label2
           and    al, 0f0h
           mov    cl, 4
           shr    al, cl
           add    al, 30h
           mov    es:[di], al
           mov    al, [si]
           and    al, 0fh
           add    al, 30h
           mov    es:[di+1], al
           inc    si
           add    di, 2
           jmp    label1

    label2:mov    ah, 4ch
           int    21h
code ends
```  
### 第二題  
Explain how the cs, ip, and the stack is modified when the instruction” CALL far ptr [DI]” is executed.（第九章）  
```=
push cs		; SP ← SP-2; [SP] ← CS
push ip		; SP ← SP-2; [SP] ← IP;
ip <- [ds:di]
cs <- [ds:di+2]
```

### 第三題  
Write a **complete** 80x86 program to read a series of 40H packed BCD numbers begin from DS:1020H. Convert theses BCD numbers into ASCII codes and store them begin at DS:2140H. Then, you use DOS function call to print out these ASCII numbers.  

```=
code segment
              assume cs:code
       start: mov    si, 1020h
              mov    di, 2140h
              mov    dl, 0
       label1:mov    al, [si]
              and    al, 0f0h
              mov    cl, 4
              shr    al, cl
              add    al, 30h
              mov    [di], al
              mov    al, [si]
              and    al, 0fh
              add    al, 30h
              mov    [di+1], al
              inc    si
              add    di, 2
              inc    dl
              cmp    dl, 40h
              jl     label1
              mov    byte ptr [di], 24h       ; 24h == $
              mov    dx, 2140h
              mov    ah, 9
              int    21h
              mov    ah, 4ch
              int    21h
code ends
stacks segment stack
              dw 64 dup(?)
stacks ends
end start
```  
### 第四題  
Write a 80x86 program segment to read a 16-bit input port at 4AH.  If the input word matches the bit pattern 10011001 01100110, then clear lower byte of bit0 and bit4 and send the lower byte to 8-bit output port A8H.  Otherwise, send F0H to the output port.（第七章）  

```=
code segment
             assume cs:code
       start:in     ax, 4Ah
             xor    ax, 9966h       ; 題目的 pattern
             jz     match
             mov    al, 0F0h
             out    0A8H, al
             jmp    exit

      match: and    al, 0EEh        ; EEh = 1110 1110
             out    0A8h, al

       exit: mov    ah, 4ch
             int    21h
code ends
```  
### 第五題  
Write a **complete** 80x86 program to read three integers x, y and z from keyboard.  Calculate (x + y) * z, and print out the expression and result. The input integers are with value between 1 and 9.  The output expression should look like “(5 + 8)*2 = 26”.  

```=
data segment
      intro1 db 9, "*** Example 5 ***",13,10,10,"*input: $"
      result db 13, 10, 20 dup("-"), 13, 10, "result: "
      num1   db "(", ?, "+", ?, ")*", ?, "="
      sum    db ?, ?, ?, 13, 10, "$"
data ends
code segment
            assume cs:code, ds:data
      start:mov    ax, data
            mov    ds, ax
            mov    si, offset num1
            mov    bx, offset sum
            mov    dx, offset intro1
            mov    ah, 9
            int    21h
            mov    ah, 1
            int    21h
            mov    [si+1], al             ; input x
            mov    ah, 1
            int    21h
            mov    [si+3], al             ; input y
            mov    ah, 1
            int    21h
            mov    [si+6], al             ; input z

            mov    al, [si+1]
            sub    al, 30h                ; 將字元轉為數字
            mov    ah, [si+3]
            sub    ah, 30h
            mov    dl, [si+6]
            sub    dl, 30h

            add    al, ah
            mul    dl
            mov    dl, 10
            div    dl
            add    ah, 30h
            mov    [bx+2], ah
            mov    ah, 0
            div    dl                     ; 因除一次得出的商（十位數）可能還是大於十，因此再處理一次進位
            add    ah, 30h
            add    al, 30h
            mov    [bx+1], ah
            mov    [bx], al

            mov    dx, offset result      ; 印出字串
            mov    ah, 9
            int    21h
            mov    ah, 4ch                ; 將控制權交還給作業系統
            int    21h
code ends
stacks segment stack
             dw 64 dup(?)
stacks ends
end start
```  
### 第六題  
Explain the internal operations of microprocessor when the following instruction is executed. That is, how the cs and ip is modified, including the operations in the stack.（第六章、第九章）  
a)	JMP   aaa  
b)	CALL far ptr [BX]  


```=
a) JMP   aaa
displacement = &aaa - IP
執行後的 IP 為當前的 IP 加上 displacement，即跳到 label aaa 的位址

b) CALL far ptr [BX]
push cs		; SP ← SP-2; [SP] ← CS
push ip		; SP ← SP-2; [SP] ← IP;
IP <- [DS:BX]
CS <- [DS:BX+2]
```  
### 第七題  
Will each of the following conditional branches taken (jump) after the compare? Your answer is yes or no.（第六章）  

> MOV AL, 92H  
> CMP AL, 0A7H  

```=
a) JE    L1    ; NO
b) JBE   L2    ; YES
c) JG    L3    ; NO
d) JO    L4    ; NO
e) JNC   L5    ; NO
```
> 大部分 conditional branch 可以由數值大小或 flag 進行判斷，下面主要以數值來判別。  

![](https://i.imgur.com/xmiLgyG.png)  

```=
code segment
	assume cs:code
	start:
		mov ax, 1200h
		mov ds, ax
		mov si, 120h
		mov ax, 3300h
		mov es, ax
		mov di, 2300h
	lp1:
		mov al, [si]
		cmp al, 0ffh
		jz exit
		add al, 30h
		mov es:[di], al
		inc si
		inc di
	exit:
		mov ah, 4ch
		int 21h
code ends
end start
```

```=
code segment
	assume cs:code
	start:
		mov ax, 1200h
		mov ds, ax
		mov si, 120h
		mov ax, 3300h
		mov es, ax
		mov di, 2300h
		mov dl, 24
	lp1:
		mov al, [si]
		sub al, 30h
		mov ah, [si+1]
		sub ah, 30h
		mov cl, 4
		shl ah, cl
		or al, ah
		mov es:[di], al
		add si, 2
		inc di
		dec dl
		cmp dl, 0
		jg lp1
		
		mov ah, 4ch
		int 21h
code ends
```