





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






; clear the screen [org 0x0100] mov ax, 0xb800 ; load video base in ax mov es, ax ; point es to video base mov di, 0 ; point di to top left column
nextchar: mov word [es:di], 0x0720 ; clear next char on screen add di, 2 ; move to next screen location cmp di, 4000 ; has the whole screen cleared jne nextchar ; if no clear next position
mov ax, 0x4c00 ; terminate program int 0x
db 0x61, 0x6 2 , 0x db 'a', 'b', 'c' db 'abc'
; hello world in assembly [org 0x0100] jmp start
message: db 'hello world' ; string to be printed length: dw 11 ; length of the string
; subroutine to clear the screen clrscr: push es push ax push di
mov ax, 0xb mov es, ax ; point es to video base mov di, 0 ; point di to top left column
nextloc: mov word [es:di], 0x0720 ; clear next char on screen add di, 2 ; move to next screen location cmp di, 4000 ; has the whole screen cleared jne nextloc ; if no clear next position
pop di pop ax pop es ret
; subroutine to print a string at top left of screen ; takes address of string and its length as parameters printstr: push bp mov bp, sp push es push ax push cx push si push di
mov ax, 0xb mov es, ax ; point es to video base mov di, 0 ; point di to top left column mov si, [bp+6] ; point si to string mov cx, [bp+4] ; load length of string in cx mov ah, 0x07 ; normal attribute fixed in al
nextchar: mov al, [si] ; load next char of string mov [es:di], ax ; show this char on screen add di, 2 ; move to next screen location add si, 1 ; move to next char in string loop nextchar ; repeat the operation cx times
pop di pop si pop cx pop ax pop es pop bp ret 4
start: call clrscr ; call the clrscr subroutine
mov ax, message push ax ; push address of message push word [length] ; push message length call printstr ; call the printstr subroutine
mov ax, 0x4c00 ; terminate program int 0x
; number printing algorithm [org 0x0100] jmp start
;;;;; COPY LINES 008-025 FROM EXAMPLE 6.2 (clrscr) ;;;;;
; subroutine to print a number at top left of screen ; takes the number to be printed as its parameter printnum: push bp mov bp, sp push es push ax push bx push cx push dx push di
mov ax, 0xb mov es, ax ; point es to video base mov ax, [bp+4] ; load number in ax mov bx, 10 ; use base 10 for division mov cx, 0 ; initialize count of digits
nextdigit: mov dx, 0 ; zero upper half of dividend div bx ; divide by 10 add dl, 0x30 ; convert digit into ascii value push dx ; save ascii value on stack inc cx ; increment count of values cmp ax, 0 ; is the quotient zero jnz nextdigit ; if no divide it again
mov di, 0 ; point di to top left column
nextpos: pop dx ; remove a digit from the stack mov dh, 0x07 ; use normal attribute mov [es:di], dx ; print char on screen add di, 2 ; move to next screen location loop nextpos ; repeat for all digits on stack
pop di pop dx pop cx pop bx pop ax pop es pop bp ret 2
start: call clrscr ; call the clrscr subroutine
mov ax, 4529 push ax ; place number on stack call printnum ; call the printnum subroutine
mov ax, 0x4c00 ; terminate program int 0x