Assembly Exp

Exp 2.1=====================================

assume cs:code
code segment
main proc far
start:
push ds
sub ax,ax
push ax

;mov ax,data
;mov ds,ax

mov cx,5h ;num of lines
mov bx,10h ;initialize

B: push cx
mov cx,10h ;num of columns


A: mov ah,02h
mov dl,bl
int 21h
mov ah,02h
mov dl,0h
int 21h
inc bl
loop A

mov ah,02h
mov dl,0dh
int 21h
mov ah,02h
mov dl,0ah
int 21h

pop cx
loop B


ret
main endp
code ends

end start

 

Exp 2.2=====================================

maxchars equ 50


stack segment

dw 100 dup(?)
se label word

stack ends

data segment

pos dw ?

s1 db 'Enter keyword:','$'
s2 db 0dh,0ah,'Enter Sentence:','$'
sM db 0dh,0ah,'Match.','$'
sNM db 0dh,0ah,'No match.','$'

maxKeys db maxchars
actKeys db ?
bufferKeys db maxchars dup(?),?

maxSen db maxchars
actSen db ?
bufferSen db maxchars dup(?),?



data ends

code segment

assume cs:code,ds:data,ss:stack

main proc far

start:
push ds
sub ax,ax
push ax

mov ax,data
mov ds,ax
mov es,ax
mov ax,stack
mov ss,ax
lea sp,se

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

mov ah,9 ;display string s1
lea dx,s1
int 21h

lea dx,maxKeys ;get key words
mov ah,0ah
int 21h

mov ah,9 ;display string s2
lea dx,s2
int 21h

lea dx,maxSen ;get sentence
mov ah,0ah
int 21h

mov al,actSen ;if actSen < actKeys go to NoMatch
sub al,actKeys
jb NoMatch

inc al
lea si,bufferKeys
lea di,bufferSen
mov bx,di
cli
mov ch,0
mov cl,al


s: push cx
mov ch,0
mov cl,actKeys
repz cmpsb

cmp cx,0
jz Match

lea si,bufferKeys

inc bx
mov di,bx

pop cx
loop s


NoMatch:mov ah,9 ;display string sNM
lea dx,sNM
int 21h

jmp gun

Match:
sub bx,offset bufferSen
mov pos,bx

mov ah,9 ;display string sM
lea dx,sM
int 21h

;======================================
gun: ret
main endp
code ends

end start



例2.3

 

;result


dataera segment
grade dw 56,69,84,82,73,88,99,63,100,80
s5 dw 0
s6 dw 0
s7 dw 0
s8 dw 0
s9 dw 0
s10 dw 0
dataera ends

program segment

main proc far

assume cs:program,ds:dataera

start:
push ds
sub ax,ax
push ax


mov ax,dataera
mov ds,ax


mov s5,0
mov s6,0
mov s7,0
mov s8,0
mov s9,0
mov s10,0
mov cx,10
mov bx,offset grade

compare:
mov ax,[bx]
cmp ax,60
jl five
cmp ax,70
jl six
cmp ax,80
jl seven
cmp ax,90
jl eight
cmp ax,100
jne nine
inc s10
jmp short change_addr
nine: inc s9
jmp short change_addr
eight: inc s8
jmp short change_addr
seven: inc s7
jmp short change_addr
six: inc s6
jmp short change_addr
five: inc s5
change_addr:
add bx,2
loop compare
ret

main endp

program ends

end start


Exp 2.4  ===========================================================

 

 


stack segment
    
    dw 100 dup(?)
se label word

stack ends

data segment
    name_buf db 21,0,21 dup(0)            ;buffer of typed name
    num_buf db 9,0,9 dup(0)                    ;buffer of typed number
    name_pos dw 0                                        ;record the next position of name in tel_tab
    num_pos dw 0                                        ;record the next position of number in tel_tab
    sort_flag db 0                                    
    sort_ax dw 0
    sort_bx dw 0
    tel_tab db 50 dup(20 dup('$'), '$',8 dup('$'),'$')
    
    sort_buf db 30 dup('0')                    ;used for exhanging data in tel_tab
    
    s1 db 0ah,'Input name:','$'
    s2 db 0dh,0ah,'Input a telephone number:','$'
    s3 db 0dh,0ah,'Do you want a telephone number?(Y/N)','$'
    s4 db 0dh,0ah,'name?','$'
    s5 db 0dh,0ah,'name            tel.',0dh,0ah,'$'
    return db 0dh,0ah,'$'
    spaces db 15 dup(' '),'$'
data ends

code segment

assume cs:code,ds:data,ss:stack
main proc far
start:
    mov ax,data
    mov ds,ax
    mov es,ax
    mov ax,stack
    mov ss,ax
    lea sp,se

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

    mov name_pos,offset tel_tab
    mov num_pos,offset tel_tab+21

t1:
    mov ah,9
    mov dx,offset s1
    int 21h
    
    call input_name
    cmp ax,1                            ;finish inputting name if 'Return' key pressed
    jz finish
    
    call stor_name
    
    mov ah,9
    mov dx,offset s2
    int 21h
    
    call inphone
    jmp t1

finish:
    mov ax,name_pos
    sub ax,30
    cmp ax,offset tel_tab                    
    jz s                                                        ;if there is only one name,no need to sort
    call name_sort

s:    
    mov ah,9
    mov dx,offset s3
    int 21h    


    mov dx,offset name_buf
    mov ah,0ah
    int 21h
    mov bx,offset name_buf+2
    mov al,byte ptr [bx]
    cmp al,'Y'

    jnz exit

t2:    
    mov ah,9
    mov dx,offset s4
    int 21h        
    
    call input_name
    
    call name_search
    
    cmp ax,0
    jz t2                                    ;go back if no results found
    
    call printline
    
    jmp s
exit:
;=====================================    
    mov ax,4c00h
    int 21h

main endp

input_name proc near
    mov dx,offset name_buf
    mov ah,0ah
    int 21h
    mov bx,offset name_buf+2
    mov al,byte ptr [bx]
    cmp al,0dh
    jz c1
    mov ax,0
    ret
c1:
    mov ax,1
    ret
input_name endp

stor_name proc near
    mov bx,offset name_buf
    inc bx
    mov ch,0
    mov cl,[bx]
    mov si,offset name_buf+2
    mov di,name_pos
    rep movsb
    add name_pos,30                        ;increase the position of next name in tel_tab
    
    ret
stor_name endp

inphone proc near

    mov dx,offset num_buf
    mov ah,0ah
    int 21h
    
    mov bx,offset num_buf
    inc bx
    mov ch,0
    mov cl,[bx]
    mov si,offset num_buf+2
    mov di,num_pos
    rep movsb
    add num_pos,30                        ;increase the position of next number in tel_tab
    
    ret
        
inphone endp

printline proc near
    mov bx,ax
    mov ah,9
    mov dx,offset s5
    int 21h
    
    mov ah,9
    mov dx,bx    
    int 21h
    
    mov ah,9
    mov dx,offset spaces
    int 21h
    
    mov ah,9
    add bx,21
    mov dx,bx
    int 21h
    
    mov ah,9
    mov dx,offset return
    int 21h
    
    ret
printline endp

name_sort proc near        ;从最后一个开始的冒泡排序
    mov ax,name_pos
    
    sub ax,30                    ;save the last name's offset in ax
    mov bx,ax
    sub bx,30                    ;save the second to last name's offset in bx
    
    mov sort_ax,ax        ;save the last name's offset in sort_ax
    mov sort_bx,bx        ;save the second to last name's offset in sort_bx

back:    
    mov si,ax
    mov di,bx
    
    cli
    
    mov cx,20
    
    repz cmpsb
    
    ja ns1                        ;don't need to exchange
    
    mov cx,30
    mov si,bx
    lea di,sort_buf
    rep movsb
    
    mov cx,30
    mov si,ax
    mov di,bx    
    rep movsb
    
    mov cx,30
    lea si,sort_buf
    mov di,ax    
    rep movsb
    
    mov sort_flag,1
ns1:
    cmp bx,offset tel_tab                    ;to determine if it's the start of tel_tab
    jz judge
    
    sub ax,30
    sub bx,30
    

    jmp back
judge:
    cmp sort_flag,0                                ;to determine if the sorting has been finished
    jz sort_over
    mov sort_flag,0
    

    mov ax,sort_ax
    mov bx,sort_bx
    
    
    jmp back
sort_over:
    
    ret

name_sort endp        

name_search proc near
    mov ch,0

    cli
    lea di,tel_tab
    mov bx,di
n1:
    mov cl,name_buf+1    
    lea si,name_buf
    add si,2
    repz cmpsb
    
    jz Match
    
    mov di,bx
    add di,30
    mov bx,di
    
    cmp byte ptr [di],24h
    jz NoMatch
    jmp n1
    

NoMatch:
    mov ax,0    

    jmp gun

Match:
    mov ax,bx        ;the offset of name found in tel_tab
    
gun:    
    ret

name_search endp

code ends

end start


例2.7 =========================================================

 

stacksg segment para stack 'stack'
dw 32 dup(?)
stacksg ends
;*************
datasg segment para'data'

hrspar label byte
maxhlen db 6
acthlen db ?
hrsfld db 6 dup(?)

ratepar label byte
maxrlen db 6
actrlen db ?
ratefld db 6 dup(?)

messg1 db 'Hours worked?','$'
messg2 db 'Rate of pay?','$'
messg3 db 'Wage ='
ascwage db 14 dup(30h),13,10,'$'
messg4 db 13,10,'Overflow!',13,10,'$'
adjust dw ?
binval dw 0
binhrs dw 0
binrate dw 0
col db 0
decind db 0
mult10 dw 01
nodec dw 0
row db 0
shift dw ?
tenwd dw 10
tempdx dw ?
tempax dw ?
datasg ends
;*******************************************
codesg segment para 'code'
;__________________________________________________
begin proc far
assume cs:codesg,ds:datasg,es:datasg,ss:stacksg

push ds
sub ax,ax
push ax

mov ax,datasg
mov ds,ax
mov es,ax

mov ax,0600h
call q10scr
call q20curs

a20loop:
call b10inpt
cmp acthlen,0
je a30
call d10hour
call e10rate
call f10mult
call g10wage
call k10disp
jmp a20loop
a30:
mov ax,0600h
call q10scr
ret
begin endp
;---------------------------------------------------
;
;
b10inpt proc near
lea dx,messg1
mov ah,09h
int 21h
lea dx,hrspar
mov ah,0ah
int 21h
cmp acthlen,0
jne b20
ret
b20: mov col,25
call q20curs
lea dx,messg2
mov ah,09h
int 21h
lea dx,ratepar
mov ah,0ah
int 21h
ret
b10inpt endp
;------------------------------------------------------------

d10hour proc near
mov nodec,0
mov cl,acthlen
sub ch,ch
lea si,hrsfld-1
add si,cx
call m10asbi
mov ax,binval
mov binhrs,ax
ret
d10hour endp
;-------------------------------

e10rate proc near
mov cl,actrlen
sub ch,ch
lea si,ratefld-1
add si,cx
call m10asbi
mov ax,binval
mov binrate,ax
ret
e10rate endp

;---------------------------------------
f10mult proc near
mov cx,07
lea di,ascwage
mov ax,3030h
cld
rep stosw

mov shift,10
mov adjust,0
mov cx,nodec
cmp cl,06
ja f40
dec cx
dec cx
jle f30
mov nodec,02
mov ax,01
f20:
mul tenwd
loop f20
mov shift,ax
shr ax,1
mov adjust,ax
f30:
mov ax,binhrs
mul binrate
add ax,adjust
adc dx,0
mov tempdx,dx
mov tempax,ax

cmp adjust,0
jz f50

mov ax,dx
mov dx,0
div shift
mov tempdx,ax
mov ax,tempax
div shift
mov dx,tempdx
mov tempax,ax
jmp f50

f40:
mov ax,0
mov dx,0

f50:
ret

f10mult endp


;--------------------------------------------------
g10wage proc near
lea si,ascwage+11
mov byte ptr[si],'.'
add si,nodec
g30:
cmp byte ptr[si],'.'
jnz g35
dec si
g35:
cmp dx,0
jnz g40
cmp ax,0010
jb g50
g40:
mov ax,dx
mov dx,0
div tenwd
mov tempdx,ax
mov ax,tempax
div tenwd
mov tempax,ax
or dl,30h
mov [si],dl
dec si
mov dx,tempdx
jmp g30
g50:
or al,30h
mov [si],al
ret
g10wage endp

;-------------------------------------------------
k10disp proc near
mov col,50
call q20curs
mov cx,10
lea si,ascwage
k20:
cmp byte ptr[si],30h
jne k30
mov byte ptr[si],20h
inc si
loop k20
k30:
lea dx,messg3
mov ah,09
int 21h
cmp row,20
jae k80
inc row
jmp k90
k80:
mov ax,0601h
call q10scr
mov col,0
call q20curs
k90:
ret
k10disp endp
;------------------------------------------------------
m10asbi proc near
mov mult10,01
mov binval,0
mov decind,0
sub bx,bx
m20:
mov al,[si]
cmp al,'.'
jne m40
mov decind,01
jmp m90
m40:
and ax,000fh
mul mult10
jc overflow
add binval,ax
jc overflow
mov ax,mult10
mul tenwd
mov mult10,ax
cmp decind,0
jnz m90
inc bx
m90:
dec si
loop m20
cmp decind,0
jz m100
add nodec,bx
jmp m100
overflow:
mov binval,0
m100:ret
m10asbi endp
;---------------------------------------------------------------
q10scr proc near
mov bh,07
sub cx,cx
mov dx,184h
int 10h
ret
q10scr endp
;------------------------------
q20curs proc near
mov ah,2
sub bh,bh
mov dh,row
mov dl,col
int 10h
ret
q20curs endp
;----------------------------------------------------
codesg ends
end begin


ball_int;====================================

 

COUNT_NUM equ 3h    ;speed of the ball moving
ROW_START equ 13 ;start position of row
COL_START equ 0 ;start positon of column
ROW_STEP equ 1
COL_STEP equ 1
EGG equ 7 ;the type of ball


stack segment

dw 100 dup(?)
se label word

stack ends

data segment
count dw COUNT_NUM
row db ROW_START ;store the position of ball in row
col db COL_START ;store the position of ball in column
rflag db 0 ;the direction of moving of row
cflag db 0 ;the direction of moving of column
data ends

code segment

assume cs:code,ds:data,ss:stack
main proc far
start:
mov ax,data
mov ds,ax
mov es,ax
mov ax,stack
mov ss,ax
lea sp,se

;=====================================
;call cls
call setcurs


mov al,1ch ;save old interrupt vector
mov ah,35h
int 21h
push es
push bx
push ds

mov dx,offset ball ;set new interrupt vector
mov ax,seg ball
mov ds,ax
mov al,1ch
mov ah,25h
int 21h


pop ds ;set interrupt mask
in al,21h
and al,11111110b
out 21h,al
sti

q:
mov di,0ffffh
delay:
mov si,0ffffh
delay1:
dec si
jnz delay1
dec di
jnz delay

jmp q

pop dx
pop ds
mov al,1ch
mov ah,25h
int 21h




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

mov ax,4c00h
int 21h

main endp


ball proc near
push ds
push ax
push cx
push dx

mov ax,data
mov ds,ax


dec count
jnz exit1

mov count,COUNT_NUM

mov bh,0
mov al,' '
mov cx,1
mov ah,0ah
int 10h


cmp rflag,0 ;determine the direction of row
je r_inc
sub row,ROW_STEP
jmp colcmp
r_inc:
add row,ROW_STEP
colcmp:
cmp cflag,0 ;determine the direction of column
je c_inc
sub col,COL_STEP
jmp tifa
c_inc:
add col,COL_STEP
tifa:
cmp rflag,0
je rcmp25
jmp rcmp0
rcmp25:
cmp row,24
jle s1
mov row,24
xor rflag,0ffh
jmp s1

exit1: ;to jump across the limit of jmp
jmp exit

rcmp0:
cmp row,0
jge s1
mov row,0
xor rflag,0ffh

s1:
cmp cflag,0
je ccmp80
jmp ccmp0
ccmp80:
cmp col,79
jle exit
mov col,79
xor cflag,0ffh
jmp exit
ccmp0:
cmp col,0
jge exit
mov col,0
xor cflag,0ffh



exit:
call setcurs

mov bh,0
mov al,EGG
mov cx,1
mov ah,0ah
int 10h


mov al,20h
out 20h,al
pop dx
pop cx
pop ax
pop ds
iret

ball endp

cls proc near
mov ax,0619h
mov bh,0
sub cx,cx
mov dx,184h
int 10h
ret
cls endp

setcurs proc near
mov ah,2
sub bh,bh
mov dh,row
mov dl,col
int 10h
ret
setcurs endp

code ends

end start


ball_sub======================================

COUNT_NUM equ 06fffh    ;speed of the ball moving
ROW_START equ 13 ;start position of row
COL_START equ 0 ;start positon of column
ROW_STEP equ 1
COL_STEP equ 1
EGG equ 'O'

stack segment

dw 100 dup(?)
se label word

stack ends

data segment
count dw COUNT_NUM
row db ROW_START
col db COL_START
rflag db 0
cflag db 0
data ends

code segment

assume cs:code,ds:data,ss:stack
main proc far
start:
mov ax,data
mov ds,ax
mov es,ax
mov ax,stack
mov ss,ax
lea sp,se

;=====================================
;call cls
call setcurs

s:
call ball

jmp s



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

mov ax,4c00h
int 21h

main endp


ball proc near
push ds
push ax
push cx
push dx

mov ax,data
mov ds,ax


dec count
jnz exit1

mov count,COUNT_NUM

mov bh,0
mov al,' '
mov cx,1
mov ah,0ah
int 10h


cmp rflag,0
je r_inc
sub row,ROW_STEP
jmp colcmp
r_inc:
add row,ROW_STEP
colcmp:
cmp cflag,0
je c_inc
sub col,COL_STEP
jmp tifa
c_inc:
add col,COL_STEP
tifa:
cmp rflag,0
je rcmp25
jmp rcmp0
rcmp25:
cmp row,24
jle s1
mov row,24
xor rflag,0ffh
jmp s1

exit1:
jmp exit

rcmp0:
cmp row,0
jge s1
mov row,0
xor rflag,0ffh

s1:
cmp cflag,0
je ccmp80
jmp ccmp0
ccmp80:
cmp col,79
jle exit
mov col,79
xor cflag,0ffh
jmp exit
ccmp0:
cmp col,0
jge exit
mov col,0
xor cflag,0ffh



exit:
call setcurs

mov bh,0
mov al,EGG
mov cx,1
mov ah,0ah
int 10h


mov al,20h
out 20h,al
pop dx
pop cx
pop ax
pop ds
ret

ball endp

cls proc near
mov ax,0620h
mov bh,0
sub cx,cx
mov dx,184h
int 10h
ret
cls endp

setcurs proc near
mov ah,2
sub bh,bh
mov dh,row
mov dl,col
int 10h
ret
setcurs endp

code ends

end start



你可能感兴趣的:(assembly)