Programming in Assembler. Laboratory manual. Exercise 6

Podobne dokumenty
PROGRAMY REZYDENTNE Terminate and State Resident, TSR

Architektura komputerów. Asembler procesorów rodziny x86

INSTRUKCJE Instrukcje przeniesienia: Instrukcje konwersji: Arytmetyczne instrukcje:

Zadanie Zaobserwuj zachowanie procesora i stosu podczas wykonywania następujących programów

Sprzęt i architektura komputerów

Adam Kotynia, Łukasz Kowalczyk

Architektura komputerów

Architektura systemów komputerowych Laboratorium 14 Symulator SMS32 Implementacja algorytmów

STRUKTURA I ORGANIZACJA STOSU (FILO- First In Last Out) Rejestry stosu: SS i SP (Stack Segment i Stack Pointer)

Programowanie niskopoziomowe

end start ; ustawienie punktu startu programu i koniec instrukcji w assemblerze.

Rejestry procesora. Nazwa ilość bitów. AX 16 (accumulator) rejestr akumulatora. BX 16 (base) rejestr bazowy. CX 16 (count) rejestr licznika

Ćwiczenie nr 3. Wyświetlanie i wczytywanie danych

PROCESORY ARM TRUDNO ZNALEŹĆ PROCESORY O TAK LICZNYCH, ORYGINALNYCH, NOWYCH, POMYSŁOWYCH ROZWIĄZANIACH!

Sterowanie pracą programu

J. Duntemann Zrozumieć Assembler Leo J. Scanlon Assembler 8086/8088/80286 S. Kruk Programowanie w Języku Assembler

architektura komputerów w 1 1

Przedmiot : Programowanie w języku wewnętrznym. Ćwiczenie nr 4

WOJSKOWA AKADEMIA TECHNICZNA

Zuzanna Hartleb i Artur Angiel

Sprzęt i architektura komputerów

Wprowadzenie do Architektury komputerów. Asembler procesorów rodziny x86

Programowanie komputera

CPU. Architektura FLAGS Bit: dr Paweł Kowalczyk; DPTNS, KFCS UŁ. SI 16 bit. 16 bit. 16 bit.

PROGRAMOWANIE NISKOPOZIOMOWE. Systemy liczbowe. Pamięć PN.01. c Dr inż. Ignacy Pardyka. Rok akad. 2011/2012

organizacja procesora 8086

Wprowadzenie do Architektury komputerów. Asembler procesorów rodziny x86

Wstęp do assemblera MA51

Procesory rodziny x86. Dariusz Chaberski

Ćwiczenie nr 6. Programowanie mieszane

PODSTAWOWE ELEMENTY ASEMBLERA TRYBY ADRESOWANIA. OPERATORY ASEMBLERA

Architektura Systemów Komputerowych, Wydział Informatyki, ZUT

Architektura systemów komputerowych Laboratorium 7 Symulator SMS32 Stos, Tablice, Procedury

Ćwiczenie 3. Konwersja liczb binarnych

Programowanie hybrydowe łączenie C/C++ z asemblerem

Akademia Górniczo- Hutmicza w Krakowie Katedra Elektroniki WIET

CPU ROM, RAM. Rejestry procesora. We/Wy. Cezary Bolek Uniwersytet Łódzki Wydział Zarządzania Katedra Informatyki

1. Asembler i wstawki asemblerowe w C

PROGRAMOWANIE NISKOPOZIOMOWE

Programowanie niskopoziomowe

Jak wiemy, wszystkich danych nie zmieścimy w pamięci. A nawet jeśli zmieścimy, to pozostaną tam tylko do najbliższego wyłączenia zasilania.

Jerzy Nawrocki, Wprowadzenie do informatyki

Architektury Komputerów - Laboratorium Informatyka III rok studia dzienne

Lista instrukcji procesora 8051 część 2 Skoki i wywołania podprogramów, operacje na stosie, operacje bitowe

Ćwiczenia 2 IBM DB2 Data Studio

Architektura Systemów Komputerowych

Procesor Intel 8086 model programisty. Arkadiusz Chrobot

Procesor Intel 8086 model programisty. Arkadiusz Chrobot

Programowanie Niskopoziomowe

Lista instrukcji mikroprocesora Programowanie w assemblerze

LCD (Liquid Crystal Display)

Pomoc do programu konfiguracyjnego RFID-CS27-Reader User Guide of setup software RFID-CS27-Reader

PROGRAMOWANIE SYSTEMÓW CZASU RZECZYWISTEGO

METHOD 2 -DIAGNOSTIC OUTSIDE

Akademia Górniczo-Hutnicza w Krakowie Katedra Elektroniki

Installation of EuroCert software for qualified electronic signature

PROGRAMOWANIE STRUKTUR CYFROWYCH. Konwencje dla asemblera KCPSM3. Simple IO. PicoBlaze - projekty. c Dr inż. Ignacy Pardyka. Rok akad.

Pamięć operacyjna. segment stosu. PUSH src - instrukcja składowania na stosie działa następująco

Struktura i działanie jednostki centralnej

Architektura systemów komputerowych Laboratorium 13 Symulator SMS32 Operacje na bitach

System przerwania (SP). Funkcje wejścia / wyjścia (I/O)

ROZDZIAŁ OSIEMNASTY: PROGRAMY REZYDENTNE

Procedury. int mult (int mcand, int mlier){ int product = 0; while (mlier > 0) { product = product + mcand; mlier = mlier -1; } return product; }

Architektura komputerów. Komputer Procesor Mikroprocesor koncepcja Johna von Neumanna

Zmiany techniczne wprowadzone w wersji Comarch ERP Altum

Architektura komputerów

Weronika Mysliwiec, klasa 8W, rok szkolny 2018/2019

petla:... ; etykieta określa adres w pamięci kodu (docelowe miejsce skoku) DJNZ R7, petla

Ćwiczenie nr 4. Zasady kodowania podprogramów

Programowanie Niskopoziomowe

Przerwania w architekturze mikrokontrolera X51

Programowanie Niskopoziomowe

Wstęp. do języka C na procesor (kompilator RC51)

5. Mikroprocesory 8086 i 8088

Rev Źródło:

SubVersion. Piotr Mikulski. SubVersion. P. Mikulski. Co to jest subversion? Zalety SubVersion. Wady SubVersion. Inne różnice SubVersion i CVS

1: ////////// 2: // test.c. 3: ssize_t ret = read(fd, buf, nbytes);

Sieciowa komunikacja procesów - XDR i RPC

RS868v3 module configuration

Jzyk C++ cz 3. Jarosław Gramacki Instytut Informatyki i Elektroniki ( $)*)+' *, - ( ' )*'.' '',*/ *, ','*0) 1 / ) %*+ 2'' 2" ( $%%) )'20 )*0) 1 / )

Testy jednostkowe - zastosowanie oprogramowania JUNIT 4.0 Zofia Kruczkiewicz

Akademia Górniczo-Hutnicza w Krakowie Katedra Elektroniki

MeetingHelper. Aplikacja Android ułatwiająca przekazywanie materiałów pomiędzy uczestnikami spotkania. Instrukcja obsługi dla programisty

LED PAR 56 7*10W RGBW 4in1 SLIM

Dyrektywy asemblera. .286p.386p.486p.586p

Język programowania C51 dla mikroprocesorów rodziny MCS51

Zaawansowane funkcje Linkera. Sekcja 13

1. Pobrać plik masm.zip (Macro Assembler 6.15 & Segmented Executable Linker 5.60) (

deep learning for NLP (5 lectures)

Programowanie mikrokontrolerów - laboratorium

MIKROPROCESORY I MIKROKONTROLERY INSTRUKCJE / KOMENDY / ROZKAZY: PRZEGLĄD I KILKA PRZYKŁADÓW DLA PRZYPOMNIENIA, GŁÓWNE REJESTRY ROBOCZE CPU:

2.1. W architekturze MIPS, na liście instrukcji widzimy dwie instrukcje dotyczące funkcji: .text main: la $a0, string1 # drukuj pierwszy łańcuch

Celem ćwiczenia jest zapoznanie z obsługą klawiatury sekwencyjnej i matrycowej w systemie DSM-51.

SQL 4 Structured Query Lenguage

How to share data from SQL database table to the OPC Server? Jak udostępnić dane z tabeli bazy SQL do serwera OPC? samouczek ANT.

INSTRUKCJE JAK AKTYWOWAĆ SWOJE KONTO PAYLUTION

ROZDZIAL SIEDEMNASTY: PRZERWANIA, PRZERWANIA KONTROLOWANE I WYJATKI

Laboratorium mikrokontrolerów

Wstęp do Reverse engineeringu

Język asembler dla każdego

Transkrypt:

Zakład Mikroinformatyki i Teorii Automatów Cyfrowych Programming in Assembler Laboratory manual Exercise 6 Interrupts handling 2008 Krzysztof Tokarz, Piotr Czekalski (edt.)

During the Exercise No.6 students are to analyze the programs using the CodeView Debugger. On the laboratory students will know some functions for interrupt handling and calling the default handlers. Programs are attached to the documentation in lab6_1.asm lab6_2.asm files. Explanation of DOS functions used in the program: 1. 25h Set interrupt vector AL = number of the interrupt; DS:DX = far address of the procedure. 2. 35h Get interrupt vector AL = number of the interrupt; Returns ES:BX = far address of the procedure. During the laboratory students are to: 1. Create the projects to the lab6_1.asm lab6_2.asm files with options for debugging and generating listing file. 2. Assemble the projects to the *.exe files and run the programs. 3. Analyze the programs with attention to methods of original interrupt handler calling. 4. Modify the program lab6_1.asm to display the ASCII code in HEX. 5. Modify the program lab6_2.asm to recognize end of the sentence with! and? characters (not only. ). The report should consist of: Title page. Explanation of programs function. Far addresses of original interrupt handlers and new interrupt handlers. Modified program listing file. Description of used interrupts (09h, 60h). Comparison of calling methods of original interrupt handler. Explanation why in the lab6_1.asm lab6_2.asm using the function number 08h of DOS interrupt 21h for reading the character is forbidden. Conclusions. Page 2 of 14

Source code (file LAB6-1.ASM): ;* LAB6-1.ASM - Assembler Laboratory ZMiTAC * ;* Sample program that displays the ASCII code of pressed key *.8086.MODEL SMALL.STACK 100h.CODE ;* Variables * old_proc dd 0 ; address of original interrupt handler ;* 09h interrupt handler * segment_kb EQU 40h ; beggining of keyboard data segment wsk_kb EQU 1Ch ; offset of pointer to keyboard buffer kb_buf_begin EQU 80h ; offset of address of begining of the buffer kb_buf_end EQU 82h ; offset of address of end of the buffer disp_segment EQU 0B800h ; adres of display data segment up_right EQU 154 ; offset of third character from left side of the screen keys PROC FAR ; Calling of original interrupt handler pushf ; push flags call old_proc ; call the interrupt hadler ; Prepare registers push ax ; push registers on the stack push bx push ds push es mov ax,segment_kb ; address of keyboard data segment to DS mov ds,ax mov ax,disp_segment ; address of display data segment to DS Page 3 of 14

mov es,ax ; Read the character and display ASCII code mov bx,ds:[wsk_kb] ; actual pointer to BX mov ax,ds:[kb_buf_begin] ; buffer beggining to AX cmp bx,ax ; is the beggining of the buffer? jne mid_buf mov bx,ds:[kb_buf_end] ; last character is at the end of the buffer mid_buf: mov ax,ds:[bx-2] ; read last character xor ah,ah ; code is in AL mov bl,10 div bl ; divide code by 10 add ah,'0' ; remainder to ASCII code mov es:[up_right+4],ah ; display ASCII code xor ah,ah div bl ; divide result by 10 add ah,'0' ; convert remainder to ASCII add al,'0' ; convert result to ASCII mov es:[up_right+2],ah ; display ASCII mov es:[up_right],al ; display ASCII ; Pop registers and return from interrupt pop es pop ds pop bx pop ax iret keys ENDP ;* Main program * ; Get interrupt start: mov ah,35h ; function 35h - read handler address mov al,09h ; of interrupt 09h mov word ptr old_proc,bx ; store 32-bit address mov word ptr old_proc+2,es ; of original interrupt handler push cs pop ds ; handler code segment to DS Page 4 of 14

mov dx,offset keys mov ah,25h mov al,09h ; offset of handler address to DX ; function 25h - set new handler ; of interrupt 09h ; Main loop looping:mov ah,08h ; function 08h - read character ; ASCII code is returned in AL cmp al,1bh ; ESC je ending ; if ESC end of the loop mov dl,al ; not ESC - move char to DL mov ah,02h ; function 02h - display character ; ASCII code of char in DL jmp looping ; Restore original interrupt handler ending: mov dx,word ptr old_proc+2 mov ds,dx mov dx,word ptr old_proc mov ah,25h ; function 25h - set old handler mov al,09h ; of interrupt 09h mov ah,4ch END start ; end of the program Page 5 of 14

Source code (file LAB6-2.ASM): ;* LAB6-2.ASM - Assembler Laboratory ZMiTAC * ;* Sample program that converts lowercase to uppercase * ;* when key pressed *.8086.MODEL SMALL.STACK 100h.CODE ;* Variables * old_proc dd 0 ; address of original interrupt handler dot_flag db 0 ; dot flag ;* 09h interrupt handler * segment_kb EQU 40h ; beggining of keyboard data segment wsk_kb EQU 1Ch ; offset of pointer to keyboard buffer kb_buf_begin EQU 80h ; offset of address of begining of the buffer kb_buf_end EQU 82h keys PROC FAR ; offset of address of end of the buffer ; Calling of original interrupt handler int 60h ; Prepare registers push ax ; push registers on the stack push bx push dx push ds mov ax,segment_kb ; address of keyboard data segment to DS mov ds,ax ; Read the character and check ASCII code mov bx,ds:[wsk_kb] ; actual pointer to BX mov ax,ds:[kb_buf_begin] ; buffer beggining to AX Page 6 of 14

cmp bx,ax ; is the beggining of the buffer? jne mid_buf mov bx,ds:[kb_buf_end] ; last character is at the end of the buffer mid_buf: mov ax,ds:[bx-2] ; read last character cmp al,'.' je dot_found cmp al,'z' ja check_lowercase cmp al,'a' jb keys_end mov dot_flag,0 jmp keys_end check_lowercase: cmp al,'z' ja keys_end cmp al,'a' jb keys_end cmp dot_flag,0 je keys_end ; compare with dot ; if dot ; compare with 'Z' ; if above check lowercase ; compare with 'A' ; end if less ; uppercase - clear flag ; return ; compare with 'z' ; end if above ; compare with 'a' ; end if less ; was dot pressed? ; end if not ; Change lowercase to uppercase sub al,'a'-'a' ; sub difference between cases mov ds:[bx-2],ax mov dot_flag,0 ; uppercase - clear flag jmp keys_end ; return dot_found: mov dot_flag,1 jmp keys_end ; set flag ; return ; Pop registers and return from interrupt keys_end: pop ds pop dx pop bx pop ax iret keys ENDP ;* Main program * Page 7 of 14

; Get interrupt start: mov ah,35h ; function 35h - read handler address mov al,09h ; of interrupt 09h mov word ptr old_proc,bx ; store 32-bit address mov word ptr old_proc+2,es ; of original interrupt handler push cs pop ds ; handler code segment to DS mov dx,offset keys ; offset of handler address to DX mov ah,25h ; function 25h - set new handler mov al,09h ; of interrupt 09h mov dx,word ptr old_proc+2 mov ds,dx mov dx,word ptr old_proc mov ah,25h ; function 25h - set new adress mov al,60h ; of original interrupt handler ; 60h instead of 09h ; Main loop looping:mov ah,08h ; function 08h - read character ; ASCII code is returned in AL cmp al,1bh ; ESC je ending ; if ESC end of the loop mov dl,al ; not ESC - move char to DL mov ah,02h ; function 02h - display character ; ASCII code of char in DL jmp looping ; Odtworzenie adresu pierwotnej procedury obslugi przerwania ending: mov dx,word ptr old_proc+2 mov ds,dx mov dx,word ptr old_proc mov ah,25h ; function 25h - set old handler mov al,09h ; of interrupt 09h mov ah,4ch END start ; end of the program Page 8 of 14

Source code (file LAB6-3.ASM): ;* LAB6-3.ASM - Assembler Laboratory ZMiTAC * ;* Sample COM program that displays actual system time *.8086.MODEL TINY ; memory model for COM programs ;* Macro that diplays two BCD digits * ;* Parameters: * ;* 8-bit register with two BCD digits * ;* address in the display memory where digits has to be displayed * BCD MACRO register,address mov al,register ; two digits to AL and al,0fh ; lower digit add al,'0' ; convert to ASCII mov es:[address][2],al ; display digit mov ah,register ; two digits to AH and ah,0f0h ; upper digit mov cl,4 ; shift 4 bits right shr ah,cl add ah,'0' ; convert to ASCII mov es:[address],ah ; display digit ENDM.CODE org 100h ; place for PSP start: jmp install ; jump over PSP ;* Variables * impulses db 0 ; interrupt counter old_proc dd 0 ; address of original interrupt handler disp_segment EQU 0B800h ; adres of display data segment up_right EQU 144 ; offset of eight character from left side of the screen ;* 1Ch interrupt handler * Page 9 of 14

clock: push ax push bx push cx push dx push ds push es mov ax,cs mov ds,ax inc impulses cmp impulses,9 jne ending ; push registers on the stack ; set DS register ; increment counter ; display every 0,5 second mov ax,disp_segment ; load display segment address to ES mov es,ax mov impulses,0 ; clear counter mov ah,02h ; read time int 1ah ; function 02 of BIOS interrupt 1Ah mov bx,cx BCD bh,up_right ; display hours BCD bl,up_right+6 ; display minuts BCD dh,up_right+12 ; display seconds ending: pop es ; restore registers pop ds pop dx pop cx pop bx pop ax ; Jump to original interrupt handler ; and return from interrupt jmp dword ptr cs:[old_proc] ;* Main program * install: ; Get interrupt mov ah,35h ; function 35h - read handler address mov al,1ch ; of interrupt 1ch mov word ptr old_proc,bx ; store 32-bit address mov word ptr old_proc+2,es ; of original interrupt handler mov dx,offset clock ; offset of handler address to DX Page 10 of 14

mov ah,25h mov al,1ch ; function 25h - set new handler ; of interrupt 1ch ; Main loop mov ah,08h ; ending address of program in the memory ; Restore interrupt mov dx,word ptr old_proc+2 mov ds,dx mov dx,word ptr old_proc mov ah,25h ; function 25h - set old handler mov al,1ch ; of interrupt 09h mov ah,4ch END start Page 11 of 14

Source code (file LAB6-4.ASM): ; Laboratorium Jezykow Asemblerowych - Cwiczenie 6 ; Zadanie 4 ; 1. Dokonac asemblacji programu. ; 2. Wykorzystujac debuger podac pelny 32-bitowy adres pierwotnej ; procedury obslugi przerwania 16h oraz procedury napisanej w ; programie. ; 3. Przeanalizowac program ze szczegolnym uwzglednieniem: ; - sposobu wywolania pierwotnej procedury obslugi przerwania ; - sposobu pozostawienia czesci programu w pamieci jako TSR ; 4. Zaprogramowac w programie z poprzedniego zadania (cw6_3.asm) ; zaiane sposobu pozostawiania ; programu w pamieci jako rezydentnego na sposob wykorzystujacy ; funkcje 31h przerwania 21h systemu MS-DOS. ;# Zamiana klawiszy Y i Z na klawiaturze # ;.8086 ;.MODEL SMALL dane SEGMENT ;# Zmienne # stara_proc dd 0 funkcja db 0 dane ENDS kod SEGMENT ASSUME cs:kod,ds:dane ;# Procedura obslugi przerwania 16h # zamiana PROC FAR mov funkcja,ah ; zapamietanie numeru wywolanej funkcji ; Wywolanie pierwotnej procedury obslugi przerwania pushf ; odlozenie na stos rejestru znacznikow call stara_proc ; wywolanie pierwotnej procedury obslugi push bp ; zapamietanie rejestru bp mov bp,sp ; aktualny adres wierzcholka stosu do bp push ax ; zapamietanie rejestru ax pushf ; przepisanie zawartosci rejestru znacznikow pop ax ; do rejestru ax mov [bp+6],ax ; modyfikacja odlozonych na stosie znacznikow pop ax ; odtworzenie rejestru ax Page 12 of 14

pop bp ; odtworzenie rejestru bp ; Sprawdzenie numeru wywolanej funkcji cmp funkcja,00h ; czy ktoras z funkcji czytajacych znak? je zamiana_yz ; jesli tak to zamiana liter cmp funkcja,10h je zamiana_yz cmp funkcja,01h je zamiana_yz cmp funkcja,11h je zamiana_yz jmp koniec ; jesli inna funkcja to powrot ; Sprawdzenie i zamiana kodu nacisnietego klawisza zamiana_yz: cmp al,'z' ; jesli nacisnieto 'z' jnz nie_z_male mov al,'y' ; to funkcja zwraca 'y' jmp koniec nie_z_male: cmp al,'y' jnz nie_y_male mov al,'z' jmp koniec nie_y_male: cmp al,'z' jnz nie_z_duze mov al,'y' jmp koniec nie_z_duze: cmp al,'y' jnz nie_y_duze mov al,'z' jmp koniec ; jesli nacisnieto 'y' ; to funkcja zwraca 'z' ; jesli nacisnieto 'Z' ; to funkcja zwraca 'Y' ; jesli nacisnieto 'Y' ; to funkcja zwraca 'Z' nie_y_duze: koniec: iret zamiana ENDP ;# Program glowny # Page 13 of 14

; Przejecie przerwania start: push ds ; zachowanie adresu segmentu naglowka PSP mov ax,dane ; zaladowanie rejestru segmentowego mov ds,ax mov ah,35h ; funkcja 35h - odczytanie adresu procedury mov al,16h ; przerwania 16h mov word ptr stara_proc,bx ; zapamietanie 32-bitowego adresu mov word ptr stara_proc+2,es ; pierwotnej procedury obslugi push cs pop ds ; do ds segment kodu w którym jest procedura mov dx,offset zamiana ; do dx offset adresu procedury mov ah,25h ; funkcja 25h - nowa procedura obslugi mov al,16h ; przerwania 16h pop ds ; odtworzenie adresu segmentu naglowka PSP ; Instalacja czesci rezydentnej mov bx,cs ; roznica adresu poczatku segmentu kodu mov ax,ds ; i adresu poczatku segmentu naglowka PSP sub bx,ax ; daje rozmiar wszystkich posrednich segmentow mov dx,offset start+15 ; adres do ktorego program zostanie w pamieci mov cl,4 ; podzielony przez 16 shr dx,cl add dx,bx ; zsumowany z roznica adresow segmentow xor al,al ; kod powrotu=0 mov ah,31h ; funkcja instalujaca czesc rezydentna kod ENDS END start ;# Koniec programu zamiany liter y i z # Page 14 of 14