Friday, 5 February 2016

Timer Tick Example of Assembly Language | Assembly Language Examples

Timer Tick Example of Assembly Language | Assembly Language Examples

Description:    An assembly language routine to display timer tick on the top right corner of display screen in the following format: HH:MM:SS



Assembly Language Code

[org 0x0100]

   jmp start

  
incTime: dw 0 
hrs: dw 0
min: dw 0
sec: dw 0

clearScreen:
  
   push ax
   push di
   push es
  
   mov ax , 0xB800
   mov es , ax
   mov di , 0
   nextCls:
      mov word [es:di] , 0x0720
      add di , 2
      cmp di , 4000
      jne nextCls
     
   pop es
   pop di
   pop ax
   ret
  
  
printTime:

   push bp
   mov bp , sp
   push ax
   push bx
   push cx
   push dx
   push si
   push di
   push es
  
   call clearScreen
  
   mov si , 0  ;counter to use 3 prints i.e. Hrs, Min, Sec
   mov ax , 0xB800
   mov es , ax
   mov di , 142
  
nextState:
  
   mov bx , bp
   sub bx , si
   add bx , 8
   mov ax , [bx]    ;BX=BP-SI+8
   mov bx , 10
   mov cx , 0
  
   nextDigit:
      mov dx , 0
      div bx
      add dl , 0x30
      push dx
      inc cx
      cmp ax , 0
      jnz nextDigit
     
   nextPos:
      pop dx
      mov dh , 0x07
      mov [es:di] , dx
      add di , 2
      loop nextPos
     
      add si , 2
      cmp si , 6
      jz return
     
      mov dl , ':'
      mov [es:di] , dx
      add di , 2
      jmp nextState
     
     
   return:
      pop es
      pop di
      pop si
      pop dx
      pop cx
      pop bx
      pop ax
      pop bp
     
      ret 6

Clock:
   push ax
  
   inc word [cs:incTime]
   cmp word [cs:incTime] , 18
   jz Reset
  
   proceedToCall:
      push word [cs:hrs]
      push word [cs:min]
      push word [cs:sec]
      call printTime
     
      mov al , 0x20 
      out 0x20 , al
     
      pop ax
      iret
     
   Reset:
      mov word [cs:incTime] , 0
      inc word [cs:sec]
      cmp word [cs:sec] , 60
      jnz proceedToCall
      mov word [cs:sec] , 0
      inc word [cs:min]
      cmp word [cs:min] , 60
      jnz proceedToCall
      mov word [cs:min] , 0
      inc word [cs:hrs]
      cmp word [cs:hrs] , 24
      jnz proceedToCall
      mov word [cs:hrs] , 0
      jmp proceedToCall
        
        
start:
   xor ax , ax
   mov es , ax
   cli
      mov word [es:8*4] , Clock
      mov word [es:8*4+2] , cs
   sti
  
   mov dx , start
   add dx , 15
   mov cl , 4
   shr dx , cl
  
   mov ax , 0x3100
   int 0x21
   

No comments

Post a Comment

Recent Posts