組合語言


回目錄


作業四解答


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

;pro41============= PROGRAM DESCRIPTION ======================

; Function Request 0FH & 14H Application. =

;==========================================================

; Function Request 0FH

; Open file

;

OPEN MACRO

LEA DX,FCB ;FCB STARTING ADDRESS

MOV AH,0FH

INT 21H

ENDM

;

;==========================================================

; Function Request 14H

; Sequential read file

;

S_READ MACRO ;讀取輸入檔之資料

LEA DX,FCB ;FCB STARTING ADDRESS

MOV AH,14H

INT 21H

ENDM

;==========================================================

PRINT MACRO BUFF ;OUTPUT STRING

MOV AH,09H

LEA DX,BUFF

INT 21H

ENDM

;==========================================================

PUTCHAR MACRO ASC ;OUTPUT 1 CHAR

MOV AH,02H

MOV DL,ASC

INT 21H

ENDM

;

.MODEL small

.STACK

CODE SEGMENT USE16

ASSUME CS:CODE,DS:CODE

ORG 5CH

FCB DB 36 DUP (?)

DTA DB 128 DUP (?)

START:

;

OPEN FCB

CMP AL,0FFH ;如果檔案開啟成功則繼續

JE ERROR ;開啟不成功則跳出並印出錯誤訊息

MOV WORD PTR FCB[32],0 ;SET CURRENT RECORD TO THE BEGINING

MOV WORD PTR FCB[14],1 ;RECORD SIZE = 1 BYTE

ABC:

S_READ FCB ;READ 1 BYTE

CMP AL,1 ;END OF FILE

JE DONE

MOV AL,DTA

CMP AL,1AH

JE DONE

PUTCHAR AL

JMP ABC

DONE:

MOV AH,4CH

INT 21H ;END OF PROGRAM

ERROR: ;開檔失敗

PRINT MSG

MOV AH,4CH

INT 21H ;END OF PROGRAM

MSG DB 'FILE NOT FOUND',0DH,0AH,'$'

CODE ENDS

END START


;****************另解*********************************;

TITLE pro41

.MODEL SMALL

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

includelib ios.lib

; Function Request 0FH

; Open file and Read

;

;==========================================================

.STACK 100h

.DATA

msg1 DB 'FILE NAME?',0

msg2 DB 'FILE NOT FOUND',0

msg3 DB 'FILE HANDLE No.=',0

File DB 36 DUP (?)

DTA DB 128 DUP (?)

.CODE

begin:

mov ax,@data ; ┐ds 初值化

mov ds,ax ; ┘

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

call puts ; ┘

call newline

mov di,OFFSET File ; ┐

call newline

call gets ; ┘

mov dx,OFFSET File ; ┐

mov ah,3dh

mov al,0h ; ┘

int 21h

JC ERROR ;開啟不成功則跳出並印出錯誤訊息

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

call puts ; ┘

call outdec ; ┘

call newline

mov bx,ax

mov dx,OFFSET DTA

mov ah,3fh

mov cx,128 ;

int 21h

mov ah,3eh

int 21h

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

call puts ; ┘

call newline

mov ah,4ch ; ┐

int 21h ; ┘

ERROR:

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

call puts ; ┘

mov ah,4ch ; ┐結束程式

int 21h ; ┘

END begin

 


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

TITLE pro42

.MODEL SMALL

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

includelib ios.lib

; Function Request 0FH

; Create file and Write

;

;==========================================================

.STACK 100h

.DATA

msg1 DB 'FILE NAME?',0

msg2 DB 'can not create this file',0

msg3 DB 'FILE HANDLE No.=',0

msg4 DB 'the input data=',0

File DB 36 DUP (?)

DTA DB 128 DUP (?)

 

.CODE

begin:

mov ax,@data ; ┐ds 初值化

mov ds,ax ; ┘

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

call puts ; ┘

call newline

mov di,OFFSET File ;

call gets ;

mov si,OFFSET File ;

call puts ;

call newline

 

mov dx,OFFSET File ;

mov ah,3ch

mov cx,02h ;

int 21h

JC ERROR ;開啟不成功則跳出並印出錯誤訊息

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

call puts ; ┘

call outdec ; ┘

call newline

mov bx,ax

mov di,OFFSET DTA

call gets

 

mov dx,OFFSET DTA

mov ah,40h

mov cx,128 ;

int 21h

mov ah,3eh

int 21h

call newline

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

call puts ; ┘

call newline

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

call puts ; ┘

call newline

mov ah,4ch ; ┐結束程式

int 21h ; ┘

ERROR:

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

call puts ; ┘

mov ah,4ch ; ┐結束程式

int 21h ; ┘

END begin

 




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

4.5題解答 (檔案較大,請直接下載.)


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

7. C語言程式
#include <stdio.h
#include <dos.h
void main(void)
{
extern BEEPON(void),BEEPOFF(void);
BEEPON();
getch();
BEEPOFF();
}

組合語言函式(喇叭發音之功能)
.MODEL small

.DATA

.STACK

.CODE

PUBLIC _BEEPON,_BEEPOFF

_BEEPON PROC far

mov al,0ffh

out 61h,al

ret

_BEEPON ENDP

_BEEPOFF PROC far

mov al,0h

out 61h,al

ret

_BEEPOFF ENDP

END

PUBLIC _BEEPON,_BEEPOFF

_TEXT SEGMENT PUBLIC 'CODE'

ASSUME CS:_TEXT

_BEEPON PROC far

mov al,0ffh

out 61h,al

ret

_BEEPON ENDP

_BEEPOFF PROC far

mov al,0h

out 61h,al

ret

_BEEPOFF ENDP

_TEXT ENDS

END
8. (a) C語言程式內含組合語言程式

#include <stdio.h
#include <dos.h
void main(void)
{
int i=2,k=3;
asm mov ax,i;
asm add ax,k;
asm mov k,ax;
printf("%d",k);
}

#include <stdio.h
#include <dos.h
void main(void)
{
int i=2,k=3;
_AX=i;
asm add ax,k;
k=_AX;
printf("%d",k);
}

(b) 以C語言程式呼叫組合語言函式
C語言程式

#include <stdio.h

#include <dos.h

void main(void)

{
extern int CAL(int,int);
int x=2,y=3,ret;
ret=CAL(x,y);
printf("%d",ret);
}

組合語言函式

.model small

.code

public _CAL ;宣告_cal是一個共用副程式

_CAL proc

push bp ;先將bp的值壓入堆疊

mov bp,sp ;將sp的值移入bp

mov ax, [bp+4] ;將第一個參數移到ax

add ax, [bp+6] ;將返回值存在ax暫存器中

pop bp ;恢復bp的值

ret

_CAL endp

end