CS401_5 by huanghengdong

VIEWS: 3 PAGES: 3

									; Minor error, read exam-ple 9.7 and solution of
; last assignment , just compine
               [org 0x0100]
               jmp start
; subroutine to print a number at top left of screen          ;
; takes the number to be printed and screen location as its parameters;

printnum:     push bp         ; Saving values of register
              mov bp, sp
              push es         ; Saving values of register
              push ax         ; Saving values of register
              push bx         ; Saving values of register
              push cx         ; Saving values of register
              push dx         ; Saving values of register
              push di        ; Saving values of register
              push si        ; Saving values of register

              mov ax, 0xb800
              mov es, ax      ; point es to video base
              mov di, [bp+4] ; loads screen location in di
              mov si, di    ; copy di into si
              mov cx, 3      ; loop count for printing zeros

printzero:    mov dh,0x07
              mov dl, '0'
              mov [es:di],dx ; printing zeros at start
              add di,2

loop printzero:      mov ax, [bp+6] ; load number in ax
                     mov bx, 10     ; use base 10 for division
                     mov cx, 0     ; initialize count of digits

nextdigit:    mov dx, 0       ; zero upper half of dividend
              div bx       ; divide by 10
              add dl, 0x30 ; convert digit into ascii value
              push dx         ; save ascii value on stack
              inc cx       ; increment count of values
              cmp ax, 0       ; is the quotient zero
              jnz nextdigit ; if no divide it again
              cmp cx, 1       ; checking digit count
              je one_digit
              cmp cx, 2       ; checking digit count
              je two_digit
              cmp cx, 3       ; checking digit count
              je nextpos
one_digit:    add si, 4      ; calculating start position for printing
              jmp nextpos

two_digit:    add si, 2    ; calculating start position for printing
              jmp nextpos
              nextpos:
              pop dx      ; remove a digit from the stack
       mov dh, 0x07 ; use normal attribute
       mov [es:si], dx ; print char on screen
       add si, 2     ; move to next screen location

loop nextpos:      ; repeat for all digits on stack
        pop si        ; retrieving old value of register
        pop di         ; retrieving old value of register
        pop dx          ; retrieving old value of register
        pop cx          ; retrieving old value of register
        pop bx          ; retrieving old value of register
        pop ax          ; retrieving old value of register
        pop es          ; retrieving old value of register
        pop bp          ; retrieving old value of register
        ret 4        ; return from function
; timer interrupt service routine

mouse:
        push ax          ; Saving values of register
        push cx          ; Saving values of register
        push dx          ; Saving values of register
        push di         ; Saving values of register
        push es          ; Saving values of register
        mov al, 1
        Int 33h       ; enabling mouse cursor
        xor cx,cx
        xor dx,dx
        mov al,3
        int 33h       ; getting (x, y) coordinates of mouse cursor
        push cx          ; pushing mouse x coordinate on stack
        mov di, 140
        push di         ; push x coordinate print location on stack
call printnum:         ; print mouse x coordinate
        mov ax, 0xb800
        mov es, ax        ; point es to video base
        mov byte[es: 146],':'; print “:” on screen
        push dx          ; pushing mouse y coordinate on stack
        mov di, 148
        push di         ; push x coordinate print location on stack
call printnum:             ; print mouse y coordinate
        mov al, 0x20
        out 0x20, al ; end of interrupt
        pop es         ; retrieving old value of register
        pop di        ; retrieving old value of register
        pop dx         ; retrieving old value of register
        pop cx         ; retrieving old value of register
        pop ax         ; retrieving old value of register
        iret        ; return from interrupt
start:
        xor ax, ax
        mov es, ax              ; point es to IVT base
        cli                     ; disable interrupts
        mov word [es:8*4], mouse               ; store offset at n*4
mov [es:8*4+2], cs   ; store segment at n*4+2
sti                    ; enable interrupts
mov dx, start        ; end of resident portion
add dx, 15           ; round up to next para
mov cl, 4
shr dx, cl           ; number of paras
mov ax, 0x3100       ; terminate and stay resident
int 0x21

								
To top