Phone : +92 302 4802033 E-mail : [email protected]
CS_401 2nd Assignment sol Spring 2019
CS_401 2nd Assignment sol Spring 2019

CS_401 2nd Assignment sol Spring 2019

                [org 0x0100]

                jmp start

oldisr: dd 0                                          ; space for saving old isr

next:     dw 0

clrscr: push es                                    ; subroutine to clear the screen

                push ax

                push cx

                push di

                mov ax, 0xb800

                mov es, ax                           ; point es to video base

                xor di, di                               ; point di to top left column

                mov ax, 0x0720                                 ; space char in normal attribute

                mov cx, 2000                      ; number of screen locations

                cld                                          ; auto increment mode

                rep stosw                            ; clear the whole screen

                pop di

                pop cx

                pop ax

                pop es

                ret

; keyboard interrupt service routine

kbisr:     push ax

                push es

                push di 

                mov di,

[next]

                add di, 1

                mov ax, 0xb800

                mov es, ax                           ; point es to video memory

                in al, 0x60                            ; read a char from keyboard port

nextcmp1:cmp al, 0x32                  ; has the m key pressed

                jne nextcmp2                     ; no, try next comparison

                mov byte [es:di], ‘m’       ; yes, print m

                jmp exit                                ; leave interrupt routine

nextcmp2: cmp al, 0x2E                                 ; has the C key pressed

                jne nextcmp3                     ; no, try next comparison

                mov byte [es:di], ‘c’         ; yes, print c

                jmp exit                                ; leave interrupt routine

nextcmp3: cmp al, 0x1F                                 ; has the s pressed

                jne nextcmp4                    ; no, try next comparison

                mov byte [es:di], ‘s’         ; print s

                jmp exit                                ; leave interrupt routine

nextcmp4: cmp al, 0x2C                                ; has the z pressed

                jne nextcmp5                    ; no, try next comparison

                mov byte [es:di], ‘z’         ; print z

                jmp exit                                ; leave interrupt routine

nextcmp5: cmp al, 0x0B                                ; has the 0 pressed

                jne nextcmp6                    ; no, try next comparison

                mov byte [es:di], ‘0’         ; print 0

                jmp exit                                ; leave interrupt routine

nextcmp6: cmp al, 0x02                                 ; has the 1 pressed

                jne nextcmp7                    ; no, try next comparison

                mov byte [es:di], ‘1’         ; print 1

                jmp exit                                ; leave interrupt routine

nextcmp7: cmp al, 0x03                                 ; has the 2 pressed

                jne nextcmp8                    ; no, try next comparison

                mov byte [es:di], ‘2’         ; print 2

                jmp exit                                ; leave interrupt routine

nextcmp8: cmp al, 0x04                                 ; has the 3 pressed

                jne nextcmp9                    ; no, try next comparison

                mov byte [es:di], ‘3’         ; print 3

                jmp exit                                ; leave interrupt routine

nextcmp9: cmp al, 0x05                                 ; has the 4 pressed

                jne nextcmpA                    ; no, try next comparison

                mov byte [es:di], ‘4’         ; print 4

                jmp exit                                ; leave interrupt routine

nextcmpA: cmp al, 0x06                                ; has the 5 pressed

                jne nextcmpB                    ; no, try next comparison

                mov byte [es:di], ‘5’         ; print 5

                jmp exit                                ; leave interrupt routine

nextcmpB: cmp al, 0x07                                ; has the 6 pressed

                jne nextcmpC                    ; no, try next comparison

                mov byte [es:di], ‘6’         ; print 6

                jmp exit                                ; leave interrupt routine

nextcmpC: cmp al, 0x08                                ; has the 7 pressed

                jne nextcmpD                    ; no, try next comparison

                mov byte [es:di], ‘7’         ; print 7

                jmp exit                                ; leave interrupt routine

nextcmpD: cmp al, 0x09                                ; has the 8 pressed

                jne nextcmpE                    ; no, try next comparison

                mov byte [es:di], ‘8’         ; print 8

                jmp exit                                ; leave interrupt routine

nextcmpE: cmp al, 0x0A                                ; has the 9 pressed

                jne nomatch                       ; no, try next comparison

                mov byte [es:di], ‘9’         ; print 9

                jmp exit                                ; leave interrupt routine

nomatch:mov [next], di

                pop di

                pop es

                pop ax

                jmp far

[cs:oldisr]

            ; call the original ISR

exit:       mov al, 0x20

                out 0x20, al                         ; send EOI to PIC              

                mov

[next]

, di

                pop di

                pop es

                pop ax

                iret                                         ; return from interrupt

start:     call clrscr                              ; call clrscr subroutine

                xor ax, ax

                mov es, ax                           ; point es to IVT base

                mov ax,

[es:9*4]

                mov

[oldisr]

, ax ; save offset of old routine

                mov ax,

[es:9*4+2]

                mov

[oldisr+2]

, ax            ; save segment of old routine

                cli                                            ; disable interrupts

                mov word [es:9*4], kbisr ; store offset at n*4

                mov

[es:9*4+2]

, cs           ; store segment at n*4+2

                sti                                           ; enable interrupts

l1:           mov ah, 0                            ; service 0 ? get keystroke

                int 0x16                                ; call BIOS keyboard service

                cmp al, 13                           ; is the Ent key pressed

                jne l1                                     ; if no, check for next key

                mov ax,

[oldisr]

; read old offset in ax

                mov bx,

[oldisr+2]

           ; read old segment in bx

                cli                                            ; disable interrupts

                mov

[es:9*4]

, ax               ; restore old offset from ax

                mov

[es:9*4+2]

, bx          ; restore old segment from bx

                sti

                mov ax, 0x4c00                 ; terminate program

                int 0x21