Este documento se presentan las estructuras utilizadas en los algoritmos en lenguaje ensamblador. Por ejemplo como es el IF , FOR, BUCLE ANIDADO , WHILE ,entre otros ...
1. INF-153 EJERCICIOS – LENGUAJE ENSAMBLADOR JOSE ALFONSO RIVERA LIMA
Códigos rápidos
3.0
INF-153
EMMU8086
HEX BIN COLOR
0 0000 black
1 0001 blue
2 0010 green
3 0011 cyan
4 0100 red
5 0101 magenta
6 0110 brown
7 0111 light gray
8 1000 dark gray
9 1001 light blue
A 1010 light green
B 1011 light cyan
C 1100 light red
D 1101 light magenta
E 1110 yellow
F 1111 white
Estas instrucciones para EMU8086, la mayoría
de las instrucciones están dadas para el
segmento de CODE SEGMENTE, caso contrario
se aclarara el código necesario.
1. Clic en NEW
2. Escoger EXE TEMPLATE y OK
Borrar las líneas de código y dejarlo listo para
programar como en la figura de alado.
2. INF-153 EJERCICIOS – LENGUAJE ENSAMBLADOR JOSE ALFONSO RIVERA LIMA
FOR 0.01 (CON EL LOOP)
Este for se itera 3 veces mov cx,03h
y mov ch,00h
Se itera 3 veces porque 03h es 3 veces en
hexadecimal.
La instrucción mov ch,00h esta indica que el cx
no se deforme, nos referimos a que si tú
quieres iterar 3 VECES deberás poner estas 2
líneas de código. Pero si solo pones la 1ra
instrucción ya no se iterara 3 VECES si no
INFINITAMENTE.
NOTA:
Si nosotros queremos iterar N>9, entonces
tendremos que darle N en HEXADECIMAL.
mov cx,03h
mov ch,00h
bucle:
lea dx,men1
mov ah,09h
int 21h
loop bucle
FOR 0.02 (CON EL LOOP)
Este for se itera 3 veces mov cl,03h
y mov ch,00h
El cl también lo puede controlar al bucle.
mov cl,03h
mov ch,00h
bucle:
lea dx,men1
mov ah,09h
int 21h
loop bucle
FOR 0.03 (CON EL JMP)
cmp cx,00h
sub cx,01h
Se resta unidad por unidad que N=3=03h.
mov cl,03h
mov ch,00h
bucle:
mov dl,31h
mov ah,02h
int 21h
cmp cx,00h
sub cx,01h
jnz bucle
FOR 0.04 (CON EL JMP) mov cx,03h
mov ch,00h
bucle:
mov dl,31h
mov ah,02h
int 21h
cmp cx,00h
sub cx,01h
jnz bucle
If
Entendiendo a sw en le sector de datos con
algún valor
cmp sw,00h; lo que queremos comparar
je sw0 ;por verdad
jne sw1 ; por falso
sw0:
;proceso
jmp salir
3. INF-153 EJERCICIOS – LENGUAJE ENSAMBLADOR JOSE ALFONSO RIVERA LIMA
sw1:
;proceso
salir:
WHILE
;while(i<=n)
;{print(1);i++}
;SIMULACION DEL WHILE
;while(i<=n)
;{print(1);i++}
data segment
n dw 0 ; de 16 bits
ends
stack segment
dw 128 dup(0)
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
mov 00h,ax
mov 01h,si
mov n,0ah ; definimos el tamano n
mov cx,01h; cx iniciamos en 1
while:
cmp cx,n ;comparamos cx<=N
jg finWhile ; si es salimos
mov dl,31h ;# 1 a imprimir
mov ah,02h ;imprimes el caracter
int 21h ;
inc cx ;incremento de cx++
jmp while
finWhile:
mov ax, 4c00h ; exit to operating system.
int 21h
ends
end start ; set entry point and stop the assembler.
FOR DE FOR(BUCLE ANIDADO)
Con registros de 16bits
; n=3 y m=3
data segment
; add your data here!
pkey db "press any key...$"
ends
4. INF-153 EJERCICIOS – LENGUAJE ENSAMBLADOR JOSE ALFONSO RIVERA LIMA
n=5 para i y m=5 para j e imprimimos 3
stack segment
dw 128 dup(0)
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
mov si,00h ; si es i=0 -- i<n=3
fori:
cmp si,03h ; n=3 -- i<n
jge endfori
mov di,00h; di es 'j' ->j=0
forj:
cmp di,03h ;m=3 -- j<m
jge endforj
call mostrar ; mostrarmos el # 3
inc di ;j++
jmp forj
endforj:;por falso si ponemos esto fuera de exit
; se repetira innitamente
inc si
jmp fori
endfori:;por falso si ponemos esto fuera de exit
; se repetira innitamente
;exit
mov ax, 4c00h ; exit to operating system.
int 21h
;exit
mostrar:
mov dl,33h
mov ah,02h
int 21h
ret
ends
end start ; set entry point and stop the assembler.
FOR DE FOR(BUCLE ANIDADO)
Con registros 8 bits
; n=3 y m=3
data segment
; add your data here!
pkey db "press any key...$"
ends
5. INF-153 EJERCICIOS – LENGUAJE ENSAMBLADOR JOSE ALFONSO RIVERA LIMA
stack segment
dw 128 dup(0)
ends
code segment
start:
; set segment registers:
mov ax, data
mov ds, ax
mov es, ax
mov cl,00h ; si es i=0 -- i<n=3
fori:
cmp cl,04h ; n=3 -- i<n
jge endfori
mov ch,00h; di es 'j' ->j=0
forj:
cmp ch,cl ;m=3 -- j<m
jge endforj
call mostrar ; mostrarmos el # 3
inc ch ;j++
jmp forj
endforj:;por falso si ponemos esto fuera de exit
; se repetira innitamente
inc cl
jmp fori
endfori:;por falso si ponemos esto fuera de exit
; se repetira innitamente
;exit
mov ax, 4c00h ; exit to operating system.
int 21h
;exit
mostrar:
mov dl,33h
mov ah,02h
int 21h
ret
ends
end start ; set entry point and stop the assembler.
POSICON_CADENA MACRO POSICON_CADENA MACRO
;FUNCION PARA IMPRIMIR LETRA EN CUALQUIER
POSICION--------
;FUNCIONARA MIETRAS NO HAYA SALTO DE LINEA
mov ah,02h ;FUNCION O INTERRUPCION
6. INF-153 EJERCICIOS – LENGUAJE ENSAMBLADOR JOSE ALFONSO RIVERA LIMA
mov bh,00h ;
mov dx,0909h ; FILA 09H Y COLUMNA 09H
int 10h
;------------------------------------------
ENDM
Mostrar CARACTERES mov dl,31h
mov ah,02h
int 21h
Mostrar CARACTERES MACRO MOSTRAR_NUMERO MACRO num
PUSH AX
PUSH DX
mov dl,num
add dl,30h
mov ah,02h
int 21h
POP DX
POP AX
ENDM
Mostrar CADENAS lea dx,men1
mov ah,09h
int 21h
Mostrar CADENAS MACRO
Al final de la cadena entre comillas s pone
$ para finalizar lan cadena.
$=Es la bandera que indica que la cadena
termina hasta donde este $
MOSTRAR_CADENA MACRO cad
PUSH AX
PUSH DX
LEA DX,cad
MOV AH,09H
INT 21H
POP DX
POP AX
ENDM
MACRO MOSTRAR NÚMEROS
(DESCENAS,UNIDAD)-[0-99]
01
.
.
.
10
.
.
.
50
.
.
.
99
imprimirNnum MACRO numm,uni,dece
push ax
push dx
mov al,numm
aam
mov uni,al
mov dece,ah
;Imprimos los tres valores empezando por decenas
y unidades.
mov ah,02h
mov dl,dece
add dl,30h
int 21h
mov dl,uni
add dl,30h
int 21h
;Termina programa
pop dx
pop ax
ENDM
7. INF-153 EJERCICIOS – LENGUAJE ENSAMBLADOR JOSE ALFONSO RIVERA LIMA
MACRO MOSTRAR NÚMEROS
(CENTENA, DECENA, UNIDAD) – [0-999]
0
.
.
.
50
.
.
.
500
.
.
.
999
imprimirNUM MACRO numm,uni,dece,cen
push ax
push dx
mov al,numm ; en F<-bl
aam ;ajusta el valor en AL por: AH=23 Y AL=4
mov uni,al ; Respaldo 4 en unidades
mov al,ah ;muevo lo que tengo en AH a AL para
poder volver a separar los números
aam ; separa lo qe hay en AL por: AH=2 Y AL=3
mov cen,ah ;respaldo las centenas en cen en este
caso 2
mov dece,al ;respaldo las decenas en dec, en este
caso 3
;Imprimos los tres valores empezando por
centenas, decenas y unidades.
mov ah,02h
mov dl,cen
add dl,30h
int 21h
mov dl,dece
add dl,30h
int 21h
mov dl,uni
add dl,30h
int 21h
pop ax
pop dx
ENDM
ESPACIO ENTRE LINEAS MACRO
Ejemplos:
Hulas as a 1 2 3
espacioElineas MACRO cad2
push ax
push dx
lea dx,cad2
mov ah,09h
int 21h
pop dx
pop ax
ENDM
;cad2 db “ $”
SALTO DE LINEA MACRO SALTO_LINEA MACRO
PUSH AX
PUSH DX
mov dl, 0dh
mov ah, 02h
int 21h
; salto de linea
mov dl, 0ah
mov ah, 02h
int 21h
POP DX
POP AX
8. INF-153 EJERCICIOS – LENGUAJE ENSAMBLADOR JOSE ALFONSO RIVERA LIMA
ENDM
COLOR_PANTALLA MACRO
Esto funcionara:
Si está dentro de los limites definidos
en cx y dx.
Caso contrario el color del texto será
por defecto BLANCO y el COLOR de
fondo será el deseado.
COLOR_PANTALLA MACRO
PUSH AX
PUSH BX
PUSH CX
PUSH DX
mov ah,06h ; peticion de recorrido de la pantalla
mov al,00h ; indica la pantalla completa
mov bh,01Fh ; attributos de color FONDO-
>01H(AZUL) y TEXTO 0f(blanco)
mov cx,0606h ; esquina superior izquierda renglon
columna
mov dx,1228h ; esquina inferior derecha renglon
columna
int 10h ; llamada a la interrupcion de video BIOS
POP DX
POP CX
POP BX
POP AX
ENDM