Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [ORG 0x100]
- [BITS 16]
- ;----------------------------------------------------------
- ; CONSTANTS
- ;----------------------------------------------------------
- vidmem equ 0xb800; Start of VGA text-memory
- scrw equ 80*25 ; Size of video memory in words
- eom equ 0 ; end of message (\0, null character like C)
- ;----------------------------------------------------------
- ; DATA
- ; Description:
- ; Here we define and reserve all of our global variables
- ;Contains:
- ; int print_start -> position where printing begins
- ; char color -> color attribute (black background, white foreground)
- ; char msg[] -> message to be printed
- ; char msg_end -> termination character for msg
- ; int msg_len -> length of msg
- ;
- ; System data and Protected Mode related data structures
- ;
- ;----------------------------------------------------------
- [SECTION .data]
- ;-----------------------------------------------------------------------------
- ;------------- APPLICATION RELATED -------------------
- ;-----------------------------------------------------------------------------
- print_start: dw 160
- color: db 7
- ex_array: db '987654321'
- msg_end: db eom
- array_len db 9
- msg_len: dw (msg_end - ex_array)
- ;----------------------------------------------------------
- ; BSS
- ; Description:
- ; Here we define our uninitialized variables
- ; Most of the time, it is used for the stack
- ; Contains:
- ; stack 256 bytes long, aligned to 4-byte limit
- ; (start address of stack can be divided by 4)
- ; stack_end -> pointer to the next memory address after the stack
- ;----------------------------------------------------------
- [SECTION .bss]
- alignb 4
- stack: resb 256
- stack_end:
- ;----------------------------------------------------------
- ; TEXT
- ; Description:
- ; The part of the file containing the code
- ; Functions:
- ; start -> entry point of the program
- ; main -> implements our algorithm
- ; cls -> Clears the screen
- ; printstr -> Prints our message
- ;----------------------------------------------------------
- [SECTION .text]
- start:
- ; Initialize the stack pointer
- mov ax,cs
- mov ss,ax
- mov sp,stack_end
- ; Set DS register to current data segment
- mov ax,cs
- mov ds,ax
- call main
- ; Return to DOS
- mov ax,0x4C00
- int 0x21
- ;----------------------------------------------------------
- ; Procedure void main()
- ; performs the main part of the program
- ; Arguments:
- ; none
- ; Local Variables:
- ; none
- ; Returns:
- ; nothing
- ;----------------------------------------------------------
- main:
- ; Make Extra Segment (ES) point to videomemory
- mov ax,vidmem
- mov es,ax
- ; Calculate where printing stops
- mov dx,scrw
- sub dx,[msg_len]
- sal dx,1
- move_msg:
- call cls
- call bubblesort
- call printstr
- ret
- ;----------------------------------------------------------
- ; Procedure void cls()
- ; clears screen
- ; Arguments:
- ; none
- ; Local Variables:
- ; none
- ; Returns:
- ; nothing
- ;----------------------------------------------------------
- cls:
- mov di,0 ; es:di point to start of vidmem
- mov al,' ' ; Lower byte of each char (char to print) = ' '
- mov ah,[color] ; Higher byte of each char (color) = white
- mov cx,scrw ; cx (repeat counter) = number of chars in screen
- ; Repear for cx times -> Store ax in es:di and increase di by 2
- rep stosw
- ret
- ;----------------------------------------------------------
- ; Procedure void printstr()
- ; prints msg on screen, by copying each character of msg
- ; at videomem starting by offset print_start.
- ; Ends copying when it encounters eom
- ; Arguments:
- ; none
- ; Local Variables:
- ; none
- ; Returns:
- ; nothing
- ;----------------------------------------------------------
- printstr:
- push si
- push di
- mov esi,ex_array ; load si with offset of msg
- mov di,160 ; load di with 160
- prints_loop:
- cmp byte [si],eom ; if current char of msg == eom
- je prints_exit ; exit
- movsb ; else mov[ds:si]->[es:di] - copy char to srceen
- ; and increase si and di
- inc di ; di now points to color byte -> increase more
- jmp prints_loop ; repeat
- prints_exit: pop di
- pop si
- ret
- ;-----------------------------BubbleSort-----------------------
- bubblesort: push esi
- push edi
- push ebx
- push ax
- push dx
- xor edi,edi
- xor esi,esi
- mov ebx,ex_array
- mov esi, array_len
- inc esi
- outerLoop: dec esi
- mov edi,0
- jz end_outer
- innerLoop: inc edi
- cmp edi,esi
- ja outerLoop
- jmp compare
- compare: mov al,byte [ebx+edi*1-1]
- mov dl, byte [ebx+edi*1]
- cmp al,dl
- jng innerLoop
- swap: mov [ebx+edi*1-1],dl
- mov [ebx+edi*1],al
- jmp innerLoop
- end_outer: pop dx
- pop ax
- pop ebx
- pop edi
- pop esi
- ret
- ;----------------------------------------------------------
- ; Procedure void software_delay()
- ;
- software_delay:
- push bx
- push cx
- mov cx,5000
- outer_Loop: mov bx,1024
- inner_Loop: dec bx
- jnz inner_Loop
- loop outer_Loop
- pop cx
- pop bx
- ret
- ;----------------------------------------------------------
- program_end:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement