martes, 12 de noviembre de 2013

PROGRAMA CON PROCEDIMIENTOS

1)
; programa que usa procedimientos

.model small
.stack
.data
  doble  db \\\'ɻȼͺ\\\'
  simple db \\\'Ú¿ÀÙij\\\'
  msj    db \\\'No se duerman$\\\'
.code
.startup

; definici¢n de procedimientos prototipos
printcad proto c, p_cad: ptr word
cuadro proto c, fi:byte, ci:byte, ff:byte, cf:byte, tipo :ptr byte
poncar proto c, letra: byte
gotoxy proto c, fil: byte,col: byte
pon_color proto c,cfi:byte, cci:byte, cff:byte, ccf:byte, color:byte


; llamada a los procedimientos
call clrscr
call pitido
invoke pon_color, 1,1,23,79,0Ah
invoke cuadro, 1,1,23,79,addr doble
invoke cuadro, 4,4,20,74,addr simple
invoke pon_color, 3,3,21,77,34h
invoke gotoxy, 10,20
invoke printcad, addr msj
invoke cuadro, 5,5,18,70,addr doble
invoke pon_color, 15,20,17,25,0A0h


.exit

; Imprime una candena de caracteres en pantalla
; en cadena recibe la direccion de 16 bits donde apunta la
; cadena a imprimir

printcad proc c, p_cad: ptr word
      mov dx,p_cad
      mov ah,09h
      int 21h
      ret
printcad endp

; emite un pitido
pitido proc near
      mov ah,02h
      mov dl,07
      int 21h
      RET
pitido endp

; limpiar pantalla

clrscr proc
   mov ah,06h         ; peticion de recorrido de la pantalla
   mov al,00h         ; indica la pantalla completa
   mov bh,07h         ; attributos de color y fondo 7 blanco 1 azul
   mov cx,0000h       ; esquina superior izquierda renglon columna
   mov dx,184fh       ; esquina inferior derecha renglon columna
   int 10h            ; llamada a la interrupcion de video BIOS
   ret
clrscr endp


;  procedimiento para el gotoxy

gotoxy proc c, fil:byte,col: byte
   mov ah,02h         ; peticion para colocar el cursor
   mov bh,00          ; numero de pagina de video 0
   mov dh,fil         ; renglon
   mov dl,col         ; columna
   int 10h            ; llamada a la interrupcion de video BIOS
   ret
gotoxy endp

; procedimiento para escribir un caracter en pantalla

poncar proc c,letra: byte
  mov ah,02h
  mov dl,letra
  int 21h
  ret
poncar endp


; procedimiento para hacer un cuadro
cuadro proc c, fi:byte,ci:byte,ff:byte,cf:byte, tipo: ptr byte
     mov BX,tipo
     mov cl,ci
     .repeat
       invoke gotoxy, fi,cl
       invoke poncar, [BX+4]
       invoke gotoxy, ff,cl
       invoke poncar, [BX+4]
       inc cl
    .until cl==cf
     mov cl,fi
     .repeat
       invoke gotoxy, cl,ci
       invoke poncar, [BX+5]
       invoke gotoxy, cl,cf
       invoke poncar, [BX+5]
       inc cl
     .until cl==ff
     invoke gotoxy, fi,ci
     invoke poncar, [BX]
     invoke gotoxy, fi,cf
     invoke poncar, [BX+1]
     invoke gotoxy, ff,ci
     invoke poncar, [BX+2]
     invoke gotoxy, ff,cf
     invoke poncar, [BX+3]
     ret
cuadro endp

; camiar color del fondo de la pantalla

pon_color proc c,cfi:byte, cci:byte, cff:byte, ccf:byte, color:byte
   mov ah,06h         ; peticion de recorrido de la pantalla
   mov al,00h         ; indica la pantalla completa
   mov bh,color       ; attributos de color y fondo 7 blanco 1 azul
   mov ch,cfi
   mov cl,cci         ; esquina superior izquierda renglon columna
   mov dh,cff
   mov dl,ccf         ; esquina inferior derecha renglon columna
   int 10h            ; llamada a la interrupcion de video BIOS
   ret
pon_color endp

end


2)

.model small
.stack
.data
        cad db 5 dup(\' \'),\'$\'
.code
.startup
        gotoxy proto c, x: byte, y: byte
        call cls
        invoke gotoxy,10,10
        call leer
        call cls
        invoke gotoxy,5,6
        call write
.exit
       
        write proc
                mov ah,09h
                lea dx,cad
                int 21h
                ret
        write endp

        leer proc
                        mov si,00
                        mov ah,01h
                        otro:
                        .repeat
                                int 21h
                                cmp al,13
                                        je salir
                                mov cad[si],al
                                inc si
                        .until (si==5)
                        salir:
                ret
                leer endp

        gotoxy proc c, x: byte, y: byte
                        mov ah,02h
                        mov dh,y
                        mov dl,x
                        mov bh,0
                        int 10h
                        ret
                gotoxy endp

        cls proc
                        mov ah,00
                        mov al,03
                        mov bh,00
                        int 10h
                        ret
                cls endp
end


PROGRAMA CON MACROS

1)
;cambiar el fondo a color azul
;poner un pixel color blanco
;usando la tecla
;usando la tecla 8,2,4,6 moveremos el pixel
GRAPH MACRO             ;iniciamos modo gráfico
                MOV AH,00H
                MOV AL,12H
                INT 10H
        ENDM

PALETA MACRO color      ;Permite definir la paleta
                MOV AH,0BH
                MOV BH,00H
                MOV BL,color
                INT 10H
        ENDM

PUNTO MACRO x,y,color
                MOV AH,0CH
                MOV AL,color    ;Color blanco
                MOV BH,0       
                MOV CX,x
                MOV DX,y
                INT 10H
        ENDM

.model small
.stack
.data
        xx dw 1
        yy dw 1
        x dw 320
        y dw 240
.code
.startup

        GRAPH           ;Seteamos modo gráfico 640*480
        PALETA 1        ;Color azul
       
        PUSH AX
        PUSH BX

        mover:
                PUNTO xx,yy,0
                PUNTO x,y,7

        MOV AH,07H      ;READKEY
        INT 21H         ;
       
        CMP AL,56; Caracter '8'
                JE UP
        CMP AL,52; Caracter '4'
                JE LEFT
        CMP AL,54; Caracter '6'
                JE RIGHT
        CMP AL,50; Caracter '2'
                JE DOWN
        CMP AL,27; Salir
                JE SALIR
                JNE mover
       
        ;UP:
        DOWN:
                MOV AX,y
                MOV yy,AX       ;Guardamos posicion anterior
                        mov bx,x;X lo movemos a XX
                        MOV xx,bx
                INC AX
                MOV y,AX
                JMP mover

        LEFT:
                MOV AX,x
                MOV xx,AX       ;Guardamos posicion anterior
                        mov bx,y;X lo movemos a XX
                        MOV yy,bx
                DEC AX
                MOV x, AX
                JMP mover      

        RIGHT:
                MOV AX,x
                MOV xx,AX       ;Guardamos posicion anterior
                        mov bx,y;X lo movemos a XX
                        MOV yy,bx
                INC AX
                MOV X, AX
                JMP mover      

        ;DOWN:
        UP:
                MOV AX,y
                MOV yy,AX       ;Guardamos posicion anterior
                        mov bx,x;X lo movemos a XX
                        MOV xx,bx
                DEC AX
                MOV y, AX
                JMP mover      

        JMP mover

        MOV AH,07H      ;READKEY
        INT 21H         ;

        SALIR:

        MOV AH,00H
        MOV AL,03H
        INT 10H
.exit
end

2)

;Programa que muestra un marco desde la coordenada (Fila1,Columna1) hasta la (Fila2,Columna2)
;El programa, utiliza macros. funciona con LOOP. y utiliza la función 02h de la interreucipon 21h
;02:15 p.m. Miercoles, 18 de Octubre de 2006
MARCO MACRO     FILA1, COL1, FILA2, COL2
                LOCAL LINE_TOP
                LOCAL EXIT_TOP
                LOCAL LINE_BOTTOM
                LOCAL EXIT_BOTTOM
                LOCAL LINE_LEFT
                LOCAL EXIT_LEFT
                LOCAL LINE_RIGHT
                LOCAL EXIT_RIGHT
                ;Recorremos desde COL2 hasta COL1 en FILA1
                MOV cl,COL2                     ;El contador decrementará desde COL2
                LINE_TOP:                       ;
                        MOV AH,02h              ;Set cursor position (Int 10/02)
                        MOV DH,fila1    ;Row
                        MOV DL,cl               ;Column
                        MOV BH,0                ;page number: 0 = in graphics modes
                        int 10h                 ;Video (Int 10)
                       
                        MOV AH,02h              ;Write attributes/characters at cursor position (Int 10/09)
                        MOV DL,'+'              ;Character
                        INT 21H                 ;
                       
                        CMP Cl,COL1
                                je EXIT_TOP
                LOOP LINE_TOP ;dec cx;cmp cx,0;jne line_left;
                EXIT_TOP:
               
                ;Recorremos desde COL2 hasta COL1 en FILA2
                MOV cl,col2
                LINE_BOTTOM:
                        MOV AH,02h              ;Set cursor position (Int 10/02)
                        MOV DH,fila2    ;Row
                        MOV DL,cl               ;Column
                        MOV BH,0                ;page number: 0 = in graphics modes
                        int 10h                 ;Video (Int 10)
                       
                        MOV AH,02h              ;Write characters only at cursor position (Int 10/0A)
                        MOV DL,'+'              ;Character
                        INT 21H                
                       
                        CMP Cl,COL1
                                je EXIT_BOTTOM
                LOOP LINE_BOTTOM ;dec cx;cmp cx,0;jne line_left;
                EXIT_BOTTOM:
               
                ;Recorremos desde FILA2 hasta FILA1 en COL1
                MOV cl,FILA2
                LINE_LEFT:
                        MOV AH,02h              ;Set cursor position (Int 10/02)
                        MOV DH,cl               ;Row
                        MOV DL,COL1             ;Column
                        MOV BH,0                ;page number: 0 = in graphics modes
                        int 10h                 ;Video (Int 10)
                       
                        MOV AH,02h              ;Write characters only at cursor position (Int 10/0A)
                        MOV DL,'+'              ;Character
                        INT 21H                
                       
                        CMP Cl,FILA1
                                je EXIT_LEFT
                LOOP LINE_LEFT ;dec cx;cmp cx,0;jne line_left;
                EXIT_LEFT:
               
                ;Recorremos desde FILA2 hasta FILA1 en COL2
                MOV cl,FILA2
                LINE_RIGHT:
                        MOV AH,02h              ;Set cursor position (Int 10/02)
                        MOV DH,cl               ;Row
                        MOV DL,COL2             ;Column
                        MOV BH,0                ;page number: 0 = in graphics modes
                        int 10h                 ;Video (Int 10)
                       
                        MOV AH,02h              ;Write characters only at cursor position (Int 10/0A)
                        MOV DL,'+'              ;Character
                        INT 21H                
                       
                        CMP Cl,FILA1
                                je EXIT_RIGHT
                LOOP LINE_RIGHT ;dec cx;cmp cx,0;jne line_left;
                EXIT_RIGHT:
               
                ;Dibujamos esquinas
                MOV AH,02h              ;Set cursor position (Int 10/02)
                MOV DH,FILA1    ;Row
                MOV DL,COL1             ;Column
                MOV BH,0                ;page number: 0 = in graphics modes
                int 10h                 ;Video (Int 10)
               
                MOV AH,02h              ;Write characters only at cursor position (Int 10/0A)
                MOV DL,'@'              ;Character
                INT 21H                
               
                MOV AH,02h              ;Set cursor position (Int 10/02)
                MOV DH,FILA1    ;Row
                MOV DL,COL2             ;Column
                MOV BH,0                ;page number: 0 = in graphics modes
                int 10h                 ;Video (Int 10)
               
                MOV AH,02h              ;Write characters only at cursor position (Int 10/0A)
                MOV DL,'@'              ;Character
                INT 21H                
               
                MOV AH,02h              ;Set cursor position (Int 10/02)
                MOV DH,FILA2    ;Row
                MOV DL,COL1             ;Column
                MOV BH,0                ;page number: 0 = in graphics modes
                int 10h                 ;Video (Int 10)
               
                MOV AH,02h              ;Write characters only at cursor position (Int 10/0A)
                MOV DL,'@'              ;Character
                INT 21H                
               
                MOV AH,02h              ;Set cursor position (Int 10/02)
                MOV DH,FILA2    ;Row
                MOV DL,COL2             ;Column
                MOV BH,0                ;page number: 0 = in graphics modes
                int 10h                 ;Video (Int 10)
               
                MOV AH,02h              ;Write characters only at cursor position (Int 10/0A)
                MOV DL,'@'              ;Character
                INT 21H                
               
        ENDM

CLS MACRO                                       ;Clear Screen
                MOV AH, 00H                     ;Set video mode
                MOV AL, 03H                     ;Video mode 80x25, 8x8, 16, 4, B800, CGA
                INT 10H                         ;Video (Int 10)
        ENDM

.model small
.stack
.code
.startup

        CLS
        MARCO 1,1,22,78
        MARCO 5,15,15,25
        MARCO 3,3,8,8
        MARCO 3,30,18,70

        ;Enviar cursor a ultima línea
        MOV AH,02h              ;Set cursor position (Int 10/02)
        MOV DX,1700H    ;Row & Col
        MOV BH,0                ;Page number: 0 = in graphics mode
        int 10h                

.exit
end
Programa que imprime 100 veces:
CODIGO FUENTE:

package imprime100veces;
public class Imprime100veces {
   
    long tiempo1 = 0;
    long tiempo2 = 0;

    public static void main(String[] args) {
        Imprime100veces programa = new Imprime100veces();
        programa.sinProcedimiento();
        //programa.conProcedimiento();
    }

    private void conProcedimiento() {
        tiempo1 = System.currentTimeMillis();
        for(int i=1;i<=100;i++){
            System.out.println(i);
        }
        tiempo2 = System.currentTimeMillis();
        System.out.println("El tiempo con procedimiento fue: " + (tiempo2-tiempo1) + " milisegundos");
    }

    private void sinProcedimiento() {
        tiempo1 = System.currentTimeMillis();
        System.out.println("1");
        System.out.println("2");
        System.out.println("3");
        System.out.println("4");
        System.out.println("5");
        System.out.println("6");
        System.out.println("7");
        System.out.println("8");
        System.out.println("9");
        System.out.println("10");
        System.out.println("11");
        System.out.println("12");
        System.out.println("13");
        System.out.println("14");
        System.out.println("15");
        System.out.println("16");
        System.out.println("17");
        System.out.println("18");
        System.out.println("19");
        System.out.println("20");
        System.out.println("21");
        System.out.println("22");
        System.out.println("23");
        System.out.println("24");
        System.out.println("25");
        System.out.println("26");
        System.out.println("27");
        System.out.println("28");
        System.out.println("29");
        System.out.println("30");
        System.out.println("31");
        System.out.println("32");
        System.out.println("33");
        System.out.println("34");
        System.out.println("35");
        System.out.println("36");
        System.out.println("37");
        System.out.println("38");
        System.out.println("39");
        System.out.println("40");
        System.out.println("41");
        System.out.println("42");
        System.out.println("43");
        System.out.println("44");
        System.out.println("45");
        System.out.println("46");
        System.out.println("47");
        System.out.println("48");
        System.out.println("49");
        System.out.println("50");
        System.out.println("51");
        System.out.println("52");
        System.out.println("53");
        System.out.println("54");
        System.out.println("55");
        System.out.println("56");
        System.out.println("57");
        System.out.println("58");
        System.out.println("59");
        System.out.println("60");
        System.out.println("61");
        System.out.println("62");
        System.out.println("63");
        System.out.println("64");
        System.out.println("65");
        System.out.println("66");
        System.out.println("67");
        System.out.println("68");
        System.out.println("69");
        System.out.println("70");
        System.out.println("71");
        System.out.println("72");
        System.out.println("73");
        System.out.println("74");
        System.out.println("75");
        System.out.println("76");
        System.out.println("77");
        System.out.println("78");
        System.out.println("79");
        System.out.println("80");
        System.out.println("81");
        System.out.println("82");
        System.out.println("83");
        System.out.println("84");
        System.out.println("85");
        System.out.println("86");
        System.out.println("87");
        System.out.println("88");
        System.out.println("89");
        System.out.println("90");
        System.out.println("91");
        System.out.println("92");
        System.out.println("93");
        System.out.println("94");
        System.out.println("95");
        System.out.println("96");
        System.out.println("97");
        System.out.println("98");
        System.out.println("99");
        System.out.println("100");
        tiempo2 = System.currentTimeMillis();
        System.out.println("El tiempo sin procedimiento fue: " + (tiempo2-tiempo1) + " milisegundos");
    }
}

EJECUCION:

run:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
El tiempo sin procedimiento fue: 12 milisegundos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
El tiempo con procedimiento fue: 6 milisegundos
BUILD SUCCESSFUL (total time: 0 seconds)

lunes, 4 de noviembre de 2013

PROGRAMA 2

Ejemplo # 2 (Implementación de la condición if-then-else) Se pretende realizar un programa en lenguaje ensamblador equivalente al siguiente código en C:
Código: 
#include <iostream.h>
main()
{
int a[] = {-12, 5, -34, -75, -10, 25};
int n = 6;
int max, i;
int npos, nneg;
for (i = npos = nneg = 0; i < n; i++)
if (a[i] > 0)
npos++;
else
nneg++;
cout << “+: “ << npos << “; -: “ << nneg << end1;
}


CODIFICACION


debug





ANALISIS

PROGRAMA 1 (Implementación de la condición if-then)
Se pretende realizar un programa en lenguaje ensamblador equivalente al siguiente código en C:

#include <iostream.h>
main()
{
int a[] = {12, 5, 34, 75, 10, 25};
int n = 6;
int max, i;
for (max = i = 0; i < n; i++)
if (a[i] > max)
max = a[i];
cout << max << end1;
}
CODIGO





ejecucion manual