You are on page 1of 6

; MASM syntax - .COM model ; SimpleIDE examples ; Filename: function.

asm ; Author : Kieu Tran Cat Son ; Copyright (C) 2004 ; CODE SEGMENT PARA PUBLIC 'CODE' ASSUME CS:CODE, DS:CODE, ES:CODE, SS:CODE ORG 100h Begin: JMP Start ; variables declare here msgGetX db 'Nhap vao x = ','$' msgGetY db 'Nhap vao y = ','$' msg db 'Hello world',0 x dw 0 y dw 0 Start: ; codes declare here lea si, msgGetX call PrintStr call ReadUInt mov x, ax call NextLine lea si, msgGetY call PrintStr call ReadUInt mov y, ax call DMAClearScr lea si, msg call StrLength mov bx, cx ; length mov al, 0eh ; yellow on black mov dx, x ; column mov cx, y ; row call DMAPrintStr call WaitKey CALL Quit Quit PROC NEAR mov ax, 4C00h INT 21h Quit ENDP NextLine PROC NEAR ; go to next line on screen mov ah, 2 mov dl, 10 int 21h mov dl, 13 int 21h ret NextLine ENDP PrintStr PROC NEAR ; print a string terminated with '$' ; <- si = offset(str) push dx

mov dx, si mov ah, 9 int 21h pop dx ret PrintStr ENDP PrintStrL PROC NEAR ; print string with length ; <- si = offset (str) ; <- cx = length (str) Print@: mov ah, 2 mov dl, ds:[si] int 21h inc si loop Print@ ret PrintStrL ENDP PrintInt PROC NEAR ; print a signed 16bit integer ; <- ax = number push si mov bx, 10 xor cx, cx xor si, si cmp ax, 0 jge DIV@ neg ax inc si DIV@: xor dx, dx div bx add dx, '0' push dx inc cx cmp ax, 0 jne DIV@ cmp si, 0 je PrintInt@ inc cx mov dx, '-' push dx PrintInt@: pop dx mov ah, 2 int 21h loop PrintInt@ pop si ret PrintInt ENDP PrintUInt PROC NEAR ; print an unsigned 16bit integer ; <- ax = number push si mov bx, 10 xor cx, cx xor si, si

UDIV@: xor dx, dx div bx add dx, '0' push dx inc cx cmp ax, 0 jne UDIV@ PrintUInt@: pop dx mov ah, 2 int 21h loop PrintUInt@ pop si ret PrintUInt ENDP WaitKey PROC NEAR ; wait until a key pressed xor ax, ax mov ah, 7 int 21h ret WaitKey ENDP ReadStr PROC NEAR ; read a string ; <- si = offset (str) ; -> cx = length (str) push si xor cx, cx ReadStr@: xor ax, ax mov ah, 7 int 21h cmp al, 13 ; Enter je EndReadStr@ cmp al, 8 ; Backspace je StrBkspc@ mov ds:[si], al inc si inc cx mov ah, 2 mov dl, al int 21h jmp ReadStr@ StrBkspc@: mov ah, 2 mov dl, 8 int 21h mov ah, 2 mov dl, 32 int 21h mov ah, 2 mov dl, 8 int 21h cmp cx, 0 je ReadStr@ dec cx dec si

jmp ReadStr@ EndReadStr@: pop si ret ReadStr ENDP ReadUInt PROC NEAR ; read an unsigned 16bit integer ; -> ax = number xor dx, dx xor cx, cx ReadUInt@: mov ah, 1 int 21h cmp al, 13 ; Enter je EndReadUInt@ cmp al, '0' jb EndReadUInt@ cmp al, '9' ja EndReadUInt@ sub al, '0' mov cl, al mov ax, dx mov bx, ax ; bx:=ax; shl ax, 1 ; ax:=ax*2 ; 2*ax shl ax, 1 ; ax:=ax*2 ; 4*ax add ax, bx ; ax:=ax+bx ; 5*ax shl ax, 1 ; ax:=ax*2 ; 10*ax add ax, cx mov dx, ax jmp ReadUInt@ EndReadUInt@: mov ax, dx ret ReadUInt ENDP ReadInt PROC NEAR ; read a signed 16bit integer ; -> ax = number push si push di xor dx, dx xor cx, cx xor si, si ; si=0 : unsign xor di, di ; di=0 : unsign ReadInt@: mov ah, 1 int 21h cmp al, 13 ; Enter je SolveInt@ cmp al, '-' ; negative je Negative@ cmp al, '+' ; positive je Positive@ cmp al, '0' jb SolveInt@ cmp al, '9' ja SolveInt@ sub al, '0' mov cl, al

mov ax, dx mov bx, ax ; bx:=ax; shl ax, 1 ; ax:=ax*2 shl ax, 1 ; ax:=ax*2 add ax, bx ; ax:=ax+bx shl ax, 1 ; ax:=ax*2 add ax, cx mov dx, ax jmp ReadInt@ Negative@: cmp dx, 0 ja SolveInt@ inc si cmp si, 1 ja SolveInt@ jmp ReadInt@ Positive@: inc di cmp si, 0 ja SolveInt@ cmp dx, 0 ja SolveInt@ cmp di, 1 ja SolveInt@ jmp ReadInt@ SolveInt@: cmp si, 0 je EndReadInt@ neg dx EndReadInt@: mov ax, dx pop di pop si ret ReadInt ENDP

; ; ; ;

2*ax 4*ax 5*ax 10*ax

StrLength PROC NEAR ; get length of an ASCIIZ string ; <- ds:si = ASCIIZ string ; -> cx = length of string push si xor cx, cx StrLen@: cmp byte ptr ds:[si],0 je EndStrLen@ inc si inc cx jmp StrLen@ EndStrLen@: pop si ret StrLength ENDP DMAPrintStr PROC NEAR ; print a string by using DMA ; <- ds:si = ASCIIZ string ; <- bx = length of string ; <- al = attribute ; <- cx = row on screen (y) ; <- dx = column on screen (x)

push es push ax mov ax, 0B800h mov es, ax dec cx mov ax, cx mov cx, 160 mul cl mov cx, ax dec dx mov ax, dx mov dx, 2 mul dl add cx, ax xchg bx, cx pop ax xor dx, dx DMAPrStrLoop@: mov dl, byte ptr ds:[si] mov byte ptr es:[bx], dl inc bx mov byte ptr es:[bx], al inc bx inc si loop DMAPrStrLoop@ pop es ret DMAPrintStr ENDP DMAClearScr PROC NEAR ; clear screen by using DMA push ds mov ax,0B800h ; video memory start at B800:0000 mov ds,ax xor bx,bx mov ax,0700h ; char #00, bgcolor=black(0), color=lightgrey(7) mov cx,07D0h ; 2000 characters DMAClearScreen@: mov WORD PTR [bx],ax add bx,2 loop DMAClearScreen@ DMAMoveCursor@: xor ax,ax ; cursor positions at 0000:0450 mov ds,ax mov cx,07 ; 8 pages mov bx,450h ; page 0 DMANextPage@: mov WORD PTR [bx],ax add bx,2 ; next page loop DMANextPage@ pop ds ret DMAClearScr ENDP CODE ENDS END Begin

You might also like