組合語言小考

回上一畫面
課程名稱:組合語言

第一次小考


1.試寫出一程式具有從Dec轉換到Hex及 Bin的功能﹐程式執行後輸入十進位整數 N即印出其Hex、及 Bin等不同的進位方式之表示結果。20%

Sol: TITLE dec transfer to hex and bin

.MODEL SMALL

includelib ios.lib

EXTRN puts:near,indec:near,outhex:near,newline:near,outbin:near

.STACK 100h

.DATA

msg1 DB 'pleaxe input a number : ',0

msg2 DB 'The hex value is : ',0

msg3 DB 'The bin value is : ',0

.CODE

begin:

mov ax,@data ; ┐ds 初值化

mov ds,ax ; ┘

mov si,OFFSET msg1 ; ┐印字串msg1

call puts ; ┘

call newline

call indec ; 輸入一個無號數值

call newline

mov bl,01h

mov si,OFFSET msg2 ; ┐印字串msg2

call puts ; ┘

call outhex ; ┘

call newline

mov si,OFFSET msg3 ; ┐印字串msg3

call puts ; ┘

call outbin ; ┘

call newline

mov ah,4ch ; ┐結束程式

int 21h ; ┘

END begin 2. 有一C語言程式如下: 20%

#include

#include

main()

{

int i,k;

for(i=1;I< 32764;i++)
k=i*1;

printf("%d",k);

}

試寫出其相對應之組合語言程式

Sol:

解一:

;*********************************

;*********************************

.MODEL small

includelib ios.lib

EXTRN newline:near,outdec:near,outhex:near,puts:near

.DATA

msg1 DB 'The value is: ',0

.CODE

start:

mov ax,@data ; ┐ds 初值化

mov ds,ax ; ┘

mov cx,01h

mov bx,01h

again:

mov ax,cx

mul bx

inc cx

cmp cx,32765

jc again

mov si,OFFSET msg1 ; ┐印字串msg1

call puts ; ┘

call outdec

mov ah,4ch

int 21h

.STACK

END start

解二:

;*********************************

;*********************************

.MODEL small

includelib ios.lib

EXTRN newline:near,outdec:near,outhex:near,puts:near

.DATA

msg1 DB 'The value is: ',0

num2 DW 07ffdh

.CODE

start:

mov ax,@data ; ┐ds 初值化

mov ds,ax ; ┘

mov cx,01h

mov bx,01h

again:

mov ax,cx

mul bx

inc cx

cmp cx,num2

jc again

mov si,OFFSET msg1 ; ┐印字串msg1

call puts ; ┘

call outdec

mov ah,4ch

int 21h

.STACK

END start

3. 試寫一摺積程式﹐程式執行後輸入五個整數a1,a2,a3,a4,a5﹐再輸入五個整

數b1,b2,b3,b4,b5﹐即印出摺積之值a1*b1+a2*b2+**..a5*b5等於若干。20%

Sol:

TITLE 計算函數的值

.MODEL SMALL

EXTRN puts:near,indec:near,outdec:near,newline:near

includelib ios.lib

.STACK 100h

.DATA

msg1 DB 'pleaxe input five numbers a1..a5 : ',0

msg2 DB 'The input five numbers a1..a5 are: ',0

msg3 DB ' ',0

msg4 DB 'pleaxe input five numbers b1..b5 : ',0

msg5 DB 'The input five numbers b1..b5 are: ',0

msg6 DB 'The value is : ',0

num1 DB 9 DUP(?)

num2 DB 9 DUP(?)

sum DW 0

.CODE

begin:

mov ax,@data ; ┐ds 初值化

mov ds,ax ; ┘

mov si,OFFSET msg1 ; ┐印出提示字串

call puts ; ┘

call newline

mov cl,05h ; 重覆輸入﹐請自行改為巨集指令

xor si,si

again1:

call indec

mov num1[si],al

inc si

call newline

loop again1

call newline

mov si,OFFSET msg2 ; ┐印出提示字串

call puts ; ┘

mov cl,05h

xor di,di

again2:

mov al,num1[di]

call outdec

inc di

mov si,OFFSET msg3 ; ┐印出提示字串

call puts ; ┘

loop again2

call newline

call newline

xor si,si

mov si,OFFSET msg4 ; ┐印出提示字串

call puts ; ┘

call newline

mov cl,05h

xor si,si

again3:

call indec

mov num2[si],al

inc si

call newline

loop again3

call newline

xor si,si

mov si,OFFSET msg5 ; ┐印出提示字串

call puts ; ┘

mov cl,05h

xor di,di

again4:

mov al,num2[di]

call outdec

inc di

mov si,OFFSET msg3 ; ┐印出提示字串

call puts ; ┘

loop again4

call newline

call newline

mov cl,05h

xor si,si

again5:

cmp al,num1[si]

jnc cont1

mov al,num1[si]

cont1:

inc si

loop again5

call newline

mov cl,05h

xor si,si

again6:

xor ax,ax

xor bx,bx

mov al,num1[si]

mov bl,num2[si]

mul bx

add sum,ax

inc si

loop again6

mov si,OFFSET msg6 ; ┐印出提示字串

call puts ; ┘

mov ax,sum

call outdec

call newline

mov ah,4ch

int 21h

END begin

4. 有一些特殊功能的暫存器:例如IP 指令指標(Intruction Pointer)暫存器與 CS 配合使用, 可追蹤程式的執行過程。 SP 堆疊指標(Stack Pointer)暫存器與 SS 配合使用,可指向目 前的堆疊位置。請問BP暫存器、SI暫存器、DI暫存器之功用為何 ?

Sol:

  BP 基礎指標(Base Pointer)暫存器可用作 SS 區段的一個相對基礎位置。

 SI 來源索引(Source Index)暫存器可用來提供相對於 DS 區段之來源指標 。

 DI 目的索引(Destination Index)暫存器可用來提供相對於 ES 區段之目的指標 。

 ( DS  用於設定資料區段(Data Segment)的位址。 SS  用於設定堆疊區段(Stack Segment) 的位址。 ES  用於設定額外區段(Extra Segment)的位址。)

5. 在DEBUG中﹐我們可以使用組合語言指令直接來編寫程式,假設現在

輸入 A100 再陸續

輸入 MOV DL,1

輸入 MOV AH,2

輸入 INT 21

輸入 INT 20

按 Enter 鍵

請問接下去用哪一個命令會在螢幕上顯示如下之訊息?

1FED:0100 B201 MOV DL,01

1FED:0102 B402 MOV AH,02

1FED:0104 CD21 INT 21

1FED:0106 CD20 INT 20

Sol:

輸入 U100,106

6. 承上題﹐在DEBUG中若我想要將此程式做成一個獨立的外部程式,以便在 DOS 的提示符號下隨 時都可以執行﹐則接下去用哪些命令來作?

Sol:

na.com

w

7. 承上題﹐在DEBUG中若我想要將此程式加以執行﹐並在螢幕上顯示Program terminated normally 之訊息﹐則接下去用哪些命令來作?

Sol:

G

8.試說明基底索引定址法﹐並說明基底索引定址法之應用場合為何?

Sol:

基底索引定址法用bx或bp作第一個相對位址﹐用si或di作第二個相對位址﹐以求得資料位址。

基底索引定址法常用以存取二維矩陣

9. CMP 指令是將兩個運算元的值加以比較﹐指令的動作很像減法,但與減法的結果有何不同?

Sol: CMP 指令的動作很像減法,是將兩個運算元的值加以比較。 執行後,暫存器或記憶體的內容 並未改變,只是相對的旗標 發生改變而已:若左邊運算元 的內含值小於右邊運算元,則正負號旗 標會 設定成 NG(-),反之則為 PL(+)。

10. CALL 指令用來呼叫一個副程式(程序),並將控制權轉移到運算元內的副程式位址,同時將 CALL 的下一指令位址定為『返回位址』,並存入堆疊中。CALL 可分為近程(NEAR)及遠程(FAR) 兩種:.NEAR用於程式與程序在同一記憶區段中。FAR 用於程式與程序在不同一記憶區段中。 試 說明其CS 暫存器與 IP 暫存器的內容放入堆疊中之順序。

Sol:

Near call :push ip. Ret:pop ip.

Far call : push cs, then push ip. Ret: pop ip then pop cs.

11.哪一個組合語言指令其功能為當零值旗標未設定,則跳到運算元所指定的記憶位址? 哪一個組合 語言指令與 CMP 指令配合,若 CMP 指令左邊運算元大於右邊運算元,則跳至其本身運算元指定的 記憶體位址?

Sol:

JNZ (Jump if Not Zero) 指令,其功能為當零值旗標未設定  ,則跳到運算元所指定的記憶位址。

JG 指令與 CMP 指令配合,若 CMP 指令左邊運算元大於右邊運算元,則跳至其本身運算元指定的 記憶體位址。

附錄一

你可以善用以下之副程式:

bin2hex...........bin2hex dec$2bin..........DEC$2BIN

dec2bin...........DEC2BIN gets..............gets

indec.............indec indec$............indec$

indec32...........indec32 indec32$..........indec32$

newline...........newline outbin............outbin

outdc32$..........outdc32$ outdec............outdec

outdec$...........outdec$ outdec32..........outdec32

outdec64..........outdec64 outhex............outhex

putc..............putc puts..............puts

各副程式功能如上課所習用者﹐例如OUTDEC 程序執行將 AX 中的 16 位元無號數以十進位顯示在 螢幕﹐而傳入值(待印值)在 AX 中。