Está en la página 1de 6

HELLO.

ASM

DOSSEG
.MODEL SMALL ; Modelo de memoria para el programa

; segmento de pila
.STACK 100h

; segmento de datos
.DATA
HelloMessage DB 'Hello, world',13,10,'$' ; Se define la cadena HelloMessage con ; el
mensaje ‘Hello, world’

; segmento de codigo
.CODE
mov ax,@data ; La dirección del segmento de datos se copia en
el ; registro ax.
mov ds,ax ; El contenido del registro ax se copia en ds. Ahora
; ds contiene la dirección de inicio del seg. De datos

mov ah,9 ; Se especifica en el registro ah la función que se


; desea ejecutar al llamarse más adelante la
; interrupción. La función 9 de DOS imprime en
; pantalla

mov dx,OFFSET HelloMessage ; copia la dirección de la cadena HelloMessage


; en el registro dx.
int 21h
; Llamada al sistema. Se ejecuta la impresión
mov ah,4ch ; en pantalla del mensaje "Hello, world".
int 21h
END ; Función para terminar un programa en DOS.
; Llamada al sistema operativo para ejecutar la
; función especificada anteriormente para terminar.

SORT - ORDENACION POR INTERCAMBIO(SORT.ASM)


PILA SEGMENT PARA STACK 'STACK' ; Reserva memoria para un stack de
DB 64 DUP('STACK ') ; 512 bytes (64*8 = 512)
PILA ENDS

DATA SEGMENT PARA PUBLIC 'DATA'


TABLE1 DB 20,19,18,17,16,15,14,13,12,11 ; Crea el arreglo TABLE1 con los
DB 10,9,8,7,6,5,4,3,2,1 ; datos a ordenarse
TOTAL DW 20 ; Crea la variable total inicializada
; con el valor 20
DATA ENDS

CSEG SEGMENT PARA PUBLIC 'CODE'

SSORT PROC FAR


ASSUME CS:CSEG,DS:DATA,SS:PILA

PUSH DS ; Salvamos la dirección de retorno


; al DOS ds : 0
SUB AX,AX
PUSH AX

MOV AX,SEG DATA ; Cargamos el segmento de datos


MOV DS,AX ; en ds.

MOV SI,OFFSET TABLE1

MOV CX,TOTAL ; Cargamos en CX el número de


; repeticiones.

SUB CX,1

DO1: ; COMIENZA LOOP (EXTERNO)


MOV DI,SI ; INICIALIZA EL INDICE J
INC DI ; I+1

DO2: ; COMIENZA LOOP (INTERNO)


MOV DL,TABLE1[SI] ; Copia la I-ésima entrada de
; TABLE1 en DL.
MOV BL,TABLE1[DI] ; Copia la J-ésima entrada de
; TABLE1 en BL.

CMP DL,BL ; Se comparan DL con BL


JNAE IIF1 ; Salta a IIF1 si DL < BL (Jump not
; above or equal)
MOV TABLE1[SI],BL ; Si la comparación anterior resultó
MOV TABLE1[DI],DL ; ser que DL > BL se deben
; intercambiar
IIF1:
INC DI ; Incrementa indice J
CMP DI,TOTAL ; Compara el indice J del loop
; interno con TOTAL para ver si se
; terminó de recorrer el arreglo
JNAE DO2 ; Si no son iguales, continua con
; el loop interno
INC SI ; SI = SI + 1
CMP SI,CX ; Compara el indice J del loop
; externo
JNA DO1 ; Si J(SI) no es mayor a CX saltar a
; DO1 continuando el loop externo
RET

SSORT ENDP
CSEG ENDS
END SSORT

COMMENT %
ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
º Fire! Routine 1.01 ÇÄ¿
ÇÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄĶ ³
º Well, here's the second demo program in the tutorial º ³
º series. Exams delayed this a bit, but all in all it took º ³
º me about a day - (11AM -> 8PM) - to do. Yes, I spent º ³
º quite a while fine tuning this. º ³
º º ³
º You are free to modify and use this code in your programs, º ³
º just don't put your name on it and redistribute it - okay? º ³
º The program can also be improved upon greatly, so if you º ³
º make something good from this - send it to me. I like to º ³
º see what people have done with my work. º ³
º º ³
º 28th June 1996 º ³
º º ³
º Contact Adam Hyde at: º ³
º þ http://www.faroc.com.au/~blackcat º ³
º þ blackcat@faroc.com.au º ³
º º ³
º (C) 1996 Adam Hyde. º ³
ÈÑÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
%

.MODEL SMALL ; Data segment < 64K, code segment < 64K
.STACK 200H ; Set up 512 bytes of stack space
.386

; ===========================================================================
.DATA

CR EQU 13
LF EQU 10

BufferX EQU 320 ; Width of screen buffer


BufferY EQU 104 ; Height of screen buffer

AllDone DB CR, LF, "That was:"


DB CR, LF
DB CR, LF, " FFFFFFFFF IIIIIII RRRRRRRRR EEEEEEEEE ! "
DB CR, LF, " FFF III RRR RRR EEE !!! "
DB CR, LF, " FFF III RRR RRR EEE !!!!!"
DB CR, LF, " FFF III RRRRRRRR EEE !!!!!"
DB CR, LF, " FFFFFFF III RRRRRRRR EEEEEEEE !!!!!"
DB CR, LF, " FFF III RRR RRR EEE !!! "
DB CR, LF, " FFF III RRR RRR EEE "
DB CR, LF, " FFF III RRR RRR EEE !!! "
DB CR, LF, " FFFFF IIIIIII RRRR RRRR EEEEEEEEE !!! "
DB CR, LF
DB CR, LF
DB CR, LF, " The demo program from Assembler Tutorial 8. You can get in touch with the"
DB CR, LF, " author, Adam Hyde, at:", CR, LF
DB CR, LF, " þ blackcat@faroc.com.au"
DB CR, LF, " þ http://www.faroc.com.au/~blackcat", CR, LF, "$"

Buffer DB BufferX * BufferY DUP (?) ; The screen buffer

Seed DW 3749h ; The seed value, and half of my


; phone number - not in hex though. :)

INCLUDE PALETTE.DAT ; The palette, generated with


; Autodesk Animator, and a simple
; Pascal program.

; ===========================================================================
.CODE

InitializeMCGA PROC
MOV AX, 0A000h
MOV ES, AX ; ES now points to the VGA

MOV AH, 00H ; Set video mode


MOV AL, 13H ; Mode 13h
INT 10H ; We are now in 320x200x256
RET
InitializeMCGA ENDP

; ---------------------------------------------------------------------------

SetUpPalette PROC
MOV SI, OFFSET Palette ; SI now points to the palette
MOV CX, 768 ; Prepare for 768 OUTs
MOV DX, 03C8H ; Palette WRITE register
XOR AL, AL ; Start at color 0
CLI ; Disable interrupts
OUT DX, AL ; Send value
CLD ; Forward direction
INC DX ; Now use palette DATA register
REP OUTSB ; 768 multiple OUTs
STI ; Enable interrupts
RET
SetupPalette ENDP

; ---------------------------------------------------------------------------

; This procedure was picked up from comp.lang.asm.x86 - many thanks to the


; unknown author.

Random PROC
MOV AX, Seed ; Move the seed value into AX
MOV DX, 8405H ; Move 8405H into DX
MUL DX ; Put 8405H x Seed into DX:AX
INC AX ; Increment AX
MOV Seed, AX ; We have a new seed
RET
Random ENDP

; ---------------------------------------------------------------------------

DrawScreen PROC
MOV SI, OFFSET Buffer ; Point SI to the start of the buffer
XOR DI, DI ; Start drawing at 0, 0
MOV BX, BufferY - 4 ; Miss the last four lines from the
; buffer. These lines will not look
; fire-like at all
Row:
MOV CX, BufferX SHR 1 ; 160 WORDS
REP MOVSW ; Move them
SUB SI, 320 ; Go back to the start of the array row
MOV CX, BufferX SHR 1 ; 160 WORDS
REP MOVSW ; Move them
DEC BX ; Decrease the number of VGA rows left
JNZ Row ; Are we finished?
RET
DrawScreen ENDP

; ---------------------------------------------------------------------------

AveragePixels PROC
MOV CX, BufferX * BufferY - BufferX * 2 ; Alter all of the buffer,
; except for the first row and
; last row
MOV SI, OFFSET Buffer + 320 ; Start from the second row

Alter:
XOR AX, AX ; Zero out AX
MOV AL, DS:[SI] ; Get the value of the current pixel
ADD AL, DS:[SI+1] ; Get the value of pixel to the right
ADC AH, 0
ADD AL, DS:[SI-1] ; Get the value of pixel to the left
ADC AH, 0
ADD AL, DS:[SI+BufferX] ; Get the value of the pixel underneath
ADC AH, 0
SHR AX, 2 ; Divide the total by four

JZ NextPixel ; Is the result zero?


DEC AX ; No, so decrement it by one

NextPixel:
MOV DS:[SI-BufferX], AL ; Put the new value into the array
INC SI ; Next pixel
DEC CX ; One less to do
JNZ Alter ; Have we done them all?
RET
AveragePixels ENDP

; ---------------------------------------------------------------------------

TextMode PROC
MOV AH, 00H ; Set video mode
MOV AL, 03H ; Mode 03h
INT 10H ; Enter 80x25x16 mode

MOV DX, OFFSET AllDone ; DS:DX points to the ending message


MOV AH, 09H
INT 21H ; Display the ending message
RET
TextMode ENDP

; ---------------------------------------------------------------------------

Start:
MOV AX, @DATA
MOV DS, AX ; DS now points to the data segment.
CALL InitializeMCGA
CALL SetUpPalette

MainLoop:
CALL AveragePixels

MOV SI, OFFSET Buffer + BufferX * BufferY - BufferX SHL 1


; SI now points to the start of the second last row
MOV CX, BufferX SHL 1 ; Prepare to get BufferX x 2 random #s

BottomLine:
CALL Random ; Get a random number
MOV DS:[SI], DL ; Use only the low byte of DX - ie,
INC SI ; the number will be 0 --> 255
DEC CX ; One less pixel to do
JNZ BottomLine ; Are we done yet?

CALL DrawScreen ; Copy the buffer to the VGA

MOV AH, 01H ; Check for keypress


INT 16H ; Is a key waiting in the buffer?
JZ MainLoop ; No, keep on going

MOV AH, 00H ; Yes, so get the key


INT 16H

CALL TextMode
MOV AH, 4CH
MOV AL, 00H
INT 21H ; Return to DOS
END Start