組合語言


回目錄

作業一解答


;pro11

TITLE 計算前N個正整數的平方和 (=n(n+1)(2n+1)/6)

.MODEL SMALL

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

.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 ; 輸入一個無號數值

mov n11,ax ; 存入變數a11

call newline ;

add ax,01h

mul n11 ; 平方

mov sum,ax ; 結果存入sum

mov ax,n11

mov bl,02h

mul bl

add ax,01h

mul sum ; 平方

mov bl,06h

div bl

mov si,OFFSET msg2 ; ┐印字串outmsg1

call puts ; ┘

call outdec ; ┘

call newline

mov ah,4ch ; ┐結束程式

int 21h ; ┘

END begin


.;pro12 : 公分轉換成公寸(=n*3937/10000)

.MODEL SMALL

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

includelib ios.lib

.STACK 100h

.DATA

msg0 DB '請輸入一個正整數:',0

msg1 DB '公分等於',0

msg2 DB '公寸',0

msg3 DB '.',0

.CODE

begin:

mov ax,@data

mov ds,ax

mov si,OFFSET msg0

call puts ;印出提示

call indec ;輸入一數

call newline

call outdec ;輸出n

mov si,OFFSET msg1

call puts ;輸出'公分等於'

mov bx,0064h

mul bx ;ax=n*100

mov bx,00feh

div bx ;ax=n*100/254的整數部份

mov bh,0h

call outdec ;輸出ax

mov si,OFFSET msg3

call puts ;輸出'.'

mov ax,dx ;ax=餘數部份

call outdec ;輸出ax

mov si,OFFSET msg2

call puts ;輸出'公寸'

mov ah,4ch

int 21h

END begin

;Pro13

TITLE 計算函數的值

.MODEL SMALL

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

.STACK 100h

.DATA

msg1 DB 'pleaxe input a number : ',0

msg2 DB 'The value is : ',0

a11 DW 0

b11 DW 0

c11 DW 0

x11 DW 0

sum DW 0

.CODE

begin:

mov ax,@data ; ┐ds 初值化

mov ds,ax ; ┘

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

call puts ; ┘

call indec ; 輸入一個無號數值

mov a11,ax ; 存入變數a11

call newline ;

call puts

call indec ; 輸入一個無號數值

mov b11,ax ; 存入變數b11

call newline ;

call puts

call indec ; 輸入一個無號數值

mov c11,ax ; 存入變數c11

call newline ;

call puts

call indec ; 輸入一個無號數值

mov x11,ax ; 存入變數x11

call newline ;

;

mul ax ; 平方

mul a11 ;

mov sum,ax ; 結果存入sum

mov ax,x11

mul b11

add ax,sum

add ax,c11

mov sum,ax ; 結果存入sum

mov si,OFFSET msg2 ; ┐印字串outmsg1

call puts ; ┘

mov ax,sum ; ┐印出sum值

call outdec ; ┘

call newline

mov ah,4ch ; ┐結束程式

int 21h ; ┘

END begin