Skip to content

Instantly share code, notes, and snippets.

@mrwonko
Created May 15, 2012 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrwonko/2700772 to your computer and use it in GitHub Desktop.
Save mrwonko/2700772 to your computer and use it in GitHub Desktop.
Assembler Project 05 - sorting the screen buffer
rem @echo off
rm ScrnSrt.lib
echo. > project.exe
echo. > project.obj
echo. > ScrnSrt.obj
masm project.asm
masm ScrnSrt.asm
lib ScrnSrt
lib ScrnSrt+ScrnSrt.obj
lib ScrnSrt
rm ScrnSrt.bak
move ScrnSrt.lib ScrnSrt2.lib
move ScrnSrt2.lib ScrnSrt.lib
link project.obj;
pause
DOSSEG
.MODEL MEDIUM
.STACK
INCLUDELIB SCRNSRT
EXTRN Sortiere:PROC
EXTRN Save:PROC
EXTRN Restore:PROC
.DATA
.CODE MainSeg
main:
call Save
call Sortiere
; Auf beliebige Taste warten
mov ah, 08h
int 21h
call Restore
mov ax, 4c00h
int 21h
END main
DOSSEG
.MODEL MEDIUM ; 1x Data, Nx Code
.STACK
; Exportierte Symbole
PUBLIC Sortiere
PUBLIC Save
PUBLIC Restore
.DATA
; Constants
SCREEN_WIDTH EQU 80
SCREEN_HEIGHT EQU 25
SCREEN_SIZE EQU SCREEN_WIDTH * SCREEN_HEIGHT
; ScreenBuffer Array
SavScreen DW SCREEN_SIZE DUP (0)
.CODE ScreenSort
Sortiere PROC
push ax
push bx
push cx
push di
push ds
push dx
; DS - to the screen buffer
mov ax, 0b800h
mov ds, ax
; iterate backwards through the screen buffer
mov cx, SCREEN_SIZE
SortL1:
push cx
; we're indexing words, so the address must be multiplied by 2
mov di, cx
sub di, 1
shl di, 1
mov ax, word ptr ds:[di]
; character value is more important than style, so swap those
push cx
mov cl, 8
rol ax, cl
pop cx
SortL2:
mov si, cx
sub si, 1
shl si, 1 ; same as di - *=2
mov bx, word ptr ds:[si]
; character value is more important than style, so swap those
push cx
mov cl, 8
rol bx, cl
pop cx
cmp ax, bx
jge SortContinue
; swap them
; rotate back
push ax
push bx
push cx
mov cl, 8
ror ax, cl
ror bx, cl
pop cx
mov word ptr ds:[di], bx
mov word ptr ds:[si], ax
pop bx
pop ax
mov ax, bx
SortContinue:
loop SortL2
pop cx
loop SortL1
pop dx
pop ds
pop di
pop cx
pop bx
pop ax
ret
Sortiere ENDP
Save PROC
push ax
push di
push ds
push es
push si
; DS
mov ax, @DATA
mov ds, ax
; DI
mov ax, OFFSET SavScreen
mov di, ax
; ES
mov ax, 0b800h
mov es, ax
; SI
xor si, si
call Copy
pop si
pop es
pop ds
pop di
pop ax
ret
Save ENDP
Restore PROC
push ax
push di
push ds
push es
push si
; DS
mov ax, 0b800h
mov ds, ax
; DI
xor di, di
; ES
mov ax, @DATA
mov es, ax
; SI
mov ax, OFFSET SavScreen
mov si, ax
call Copy
pop si
pop es
pop ds
pop di
pop ax
ret
Restore ENDP
;
; PRIVATE
;
; Copies SCREEN_SIZE values from ds:di to es:si
Copy PROC
push ax
push cx
mov cx, SCREEN_SIZE
SaveLoop:
mov ax, es:[si]
mov word ptr ds:[di], ax
add di, 2
add si, 2
loop SaveLoop
pop cx
pop ax
ret
Copy ENDP
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment