Assembly Lanuage Programming
|
|
|
|
|
|
|
|
|
|
i. puts data at sp into ax
ii. sp ß sp + 2
Address Data before Data after
sp à SS:003ah xx xx
SS:0039h yy 34
SS:0038h zz sp à 12
SS:0037h
routine: push ax
push bx
push cx
< code which uses ax,bx,cx >
pop cx
pop bx
pop ax
ret
mov ax,5 ; load ax register with the count
l1: call print ; ax must not be destroyed
; in this call
dec ax ; update counter
jnz l1 ; jump if not zero
for (a = 5; a != 0; a--)
print();
mov cx,5
l1: call print ; assume cx not destroyed
loop l1
jl l2 ; jumps if cl < 10 (cl not affected)
test cx,4h
jnz bitset
shl ax,1
mov ax,bx
shl ax,2
add ax,bx ; now ax = 10 * bx
; (dx is a port addr)
les di, buffer ; loads es:di buffer addr
mov cx,5 ; we will use words - faster
mov ax,0
cld ; autoincrement mode
rep stosw ; does "stosw" cx(=5) times
mov ah,08h
int 21h ; returns with char in al
.MODEL SMALL
.STACK 200h
.DATA
Message db "This was printed using function 9 "
db "of the DOS interrupt 21h.$"
.CODE
START:
mov ax,seg Message ;moves SEGMENT of `Message' to AX
mov ds,ax ;moves ax into ds (ds=ax)
;you cannot do this -> mov ds,seg Message
mov dx,offset Message ;move OFFSET of `Message' to DX
mov ah,9 ;DOS Function 9, int 21h prints a string that
int 21h ;terminates with a "$". Requires FAR pointer to
;what is to be printed in DS:DX
mov ax,4c00h ;Returns control to DOS
int 21h ;MUST be here! Program will crash without it!
END START
buf1 db bufz dup (?)
buf2 db bufz dup (?)
pushregs macro
push ax
push bx
push cx
push dx
push si
push di
endm
xx macro reg
local l1
push dx
l1: …
jmp l1
endm
2 0000 .MODEL SMALL
3 0000 .STACK 200h
4 =0064 bufz equ 100
5
6 pushregs macro
7 push ax
8 push bx
9 push cx
10 push dx
11 push si
12 push di
13 endm
14 ; macro with parameters
15 addmac macro x,y,res
16 push ax ; saves ax
17 mov ax,x
18 add ax,y
19 mov res,ax
20 pop ax
21 endm
22
23 0000 .DATA
24 org 200h
25 0200 A0 0A ?? 0A 48 65 6C+ db 0a0h, 1010b, ?, 10, "Hello", 0
26 6C 6F 00
27 020A 0001 0002 0003 dw 1, 2, 3
28 0210 64*(??) buf1 db bufz dup (?)
29 0274 64*(??) buf2 db bufz dup (?)
30
31 02D8 .CODE
32
33 0000 START: pushregs
1 34 0000 50 push ax
1 35 0001 53 push bx
1 36 0002 51 push cx
1 37 0003 52 push dx
1 38 0004 56 push si
1 39 0005 57 push di
40 addmac 2,4,bx
1 41 0006 50 push ax ; saves ax
1 42 0007 B8 0002 mov ax,2
1 43 000A 05 0004 add ax,4
1 44 000D 8B D8 mov bx,ax
1 45 000F 58 pop ax
46 END START
; this a procedure to print a block on the screen using
; registers to pass parameters (cursor position of where to
; print it and colour).
.model tiny
.code
org 100h
Start:
mov dh,4 ; row to print character on
mov dl,5 ; column to print character on
mov al,254 ; ascii value of block to display
mov bl,4 ; colour to display character
call PrintChar ; print our character
mov ax,4C00h ; terminate program
int 21h
PrintChar PROC NEAR
push bx ; save registers to be destroyed
push cx
xor bh,bh ; clear bh - video page 0
mov ah,2 ; function 2 - move cursor
int 10h ; row and col are already in dx
pop bx ; restore bx
xor bh,bh ; display page - 0
mov ah,9 ; function 09h write char & attrib
mov cx,1 ; display it once
int 10h ; call bios service
pop cx ; restore registers
ret ; return to where it was called
PrintChar ENDP
end Start
.model tiny
.code
org 100h
Start:
mov Row,4 ; row to print character
mov Col,5 ; column to print character on
mov Char,254 ; ascii value of block to display
mov Colour,4 ; colour to display character
call PrintChar ; print our character
mov ax,4C00h ; terminate program
int 21h
PrintChar PROC NEAR
push ax cx bx ; save registers to be destroyed
xor bh,bh ; clear bh - video page 0
mov ah,2 ; function 2 - move cursor
mov dh,Row
mov dl,Col
int 10h ; call Bios service
mov al,Char
mov bl,Colour
xor bh,bh ; display page - 0
mov ah,9 ; function 09h write char & attrib
mov cx,1 ; display it once
int 10h ; call bios service
pop bx cx ax ; restore registers
ret ; return to where it was called
PrintChar ENDP
; variables to store data
Row db ?
Col db ?
Colour db ?
Char db ?
end Start

.model tiny
.code
org 100h
Start:
mov dh,4 ; row to print string on
mov dl,5 ; column to print string on
mov al,254 ; ascii value of block to display
mov bl,4 ; colour to display character
push dx ax bx ; put parameters onto the stack
call PrintString ; print our string
pop bx ax dx ;restore registers
mov ax,4C00h ;terminate program
int 21h
PrintString PROC NEAR
push bp ; save bp
mov bp,sp ; put sp into bp
push cx ; save registers to be destroyed
xor bh,bh ; clear bh - video page 0
mov ah,2 ; function 2 - move cursor
mov dx,[bp+8] ; restore dx
int 10h ; call bios service
mov ax,[bp+6] ; character
mov bx,[bp+4] ; attribute
xor bh,bh ; display page - 0
mov ah,9 ; function 09h write char & attrib
mov cx,1 ; display it once
int 10h ; call bios service
pop cx ; restore registers
pop bp
ret ; return to where it was called
PrintString ENDP
end Start
public var1
.data
dw var1 ; need an extrn var1:word to use this in the other file