組合語言


回目錄

作業二解答


;Pro21

*********************************

; ASCII.ASM

; 印出從Dec30到Dec120的ASCII字元對照表

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

.MODEL small

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

.DATA

msg1 DB 'Dec: ',0

msg2 DB ' Hex: ',0

msg3 DB ' ASCII: ',0

.CODE

start:

mov ax,@data ; ┐ds 初值化

mov ds,ax ; ┘

mov cl,5Ah

again: mov al,79h

sub al,cl

call newline

mov ah,00h

mov si,OFFSET msg1 ; ┐印字串msg1

call puts ; ┘

call outdec

mov si,OFFSET msg2 ; ┐印字串msg2

call puts ; ┘

call outhex

mov ah,02h

mov dl,al

mov si,OFFSET msg3 ; ┐印字串msg3

call puts ; ┘

int 21h

LOOP again

mov ah,4ch

int 21h

.STACK

END start


;pro22

TITLE 計算函數的值

.MODEL SMALL

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

includelib ios.lib

.STACK 100h

.DATA

msg1 DB 'pleaxe input nine numbers : ',0

msg2 DB 'The input nine numbers are: ',0

msg3 DB ' ',0

msg4 DB 'The maximum value is : ',0

msg5 DB 'The minimum value is : ',0

num1 DB 9 DUP(?)

.CODE

begin:

mov ax,@data ; ┐ds 初值化

mov ds,ax ; ┘

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

call puts ; ┘

call newline

mov cl,09h

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,09h

xor di,di

again2:

mov al,num1[di]

call outdec

inc di

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

call puts ; ┘

loop again2

call newline

call newline

mov cl,09h

xor si,si

again3:

cmp al,num1[si]

jnc cont1

mov al,num1[si]

cont1:

inc si

loop again3

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

call puts ; ┘

call outdec

call newline

mov cl,09h

xor si,si

again4:

cmp al,num1[si]

jc cont2

mov al,num1[si]

cont2:

inc si

loop again4

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

call puts ; ┘

call outdec

call newline

mov ah,4ch

int 21h

END begin


TITLE pro24

.MODEL SMALL

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

includelib ios.lib

.STACK 100h

.DATA

msg1 DB 'pleaxe input nine numbers a1..a9 : ',0

msg2 DB 'The input nine numbers a1..a9 are: ',0

msg3 DB ' ',0

msg4 DB 'pleaxe input nine numbers b1..b9 : ',0

msg5 DB 'The input nine numbers b1..b9 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,09h

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,09h

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,09h

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,09h

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,09h

xor si,si

again5:

cmp al,num1[si]

jnc cont1

mov al,num1[si]

cont1:

inc si

loop again5

call newline

mov cl,09h

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

;pro25

TITLE 計算16位元變數中位元為1之個數有多少個

.MODEL SMALL

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

includelib ios.lib

.STACK 100h

.DATA

msg1 DB 'pleaxe input a number : ',0

msg2 DB 'The value is : ',0

n11 DW 0

sum DW 0

.CODE

begin:

mov ax,@data ; ┐ds 初值化

mov ds,ax ; ┘

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

call puts ; ┘

call indec ; 輸入一個無號數值

call newline

mov cx,10h ;SET SHIFT TIMES

mov bl,0 ;INITIAL COUNT

again:

shl ax,1 ;SHIFT LEFT ONE BIT

jnc next ;IF NOT 1 BIT GO TO NEXT

inc bl ;INCREASE THE COUNT

next:

loop again ;LOOP UNTIL ALL BIT ARE CHECKED

mov al,bl ;STORE THE COUNT

mov si,OFFSET msg2 ; ┐印字串outmsg1

call puts ; ┘

call newline

call outdec ; ┘

call newline

mov ah,4ch ; ┐結束程式

int 21h ; ┘

END begin

pro27:

1FED:0100 MOV CX,0100

1FED:0103 MOV DL,00

1FED:0105 MOV AH,02

1FED:0107 INT 21

1FED:0109 INC DL

1FED:010B LOOP 0105

1FED:010D INT 20