www.pudn.com > Time1.rar > Time1.asm


;========================================== 
;A little assembly app that shows the current date and time. 
;It can be done a lot easier, but this way you will  
;see how to do some basic memory manipulation, and how to use 'variables'. 
;========================================== 
 
.model small 
 .stack 
 
;=========================== 
;Data segment starts here 
;=========================== 
 .data 
date_str db "Current date is: yyyy-mm-dd", 0Ah, 0Dh, "$" 
time_str db "Current time is: hh.mm.ss:xx", 0Ah, 0Dh, "$" 
min_size dw ? 
padd_chr db  ? 
 
 
;=========================== 
;Code segment starts here 
;=========================== 
 .code 
main proc 
 mov ax, seg @data  ;First we get the data segment address 
 mov ds, ax   ;and store it into ds 
 
 mov [min_size], 02h  ;Results should always be at least two digits 
 mov [padd_chr], '0'  ;Use '0' as padding-character 
 
 mov ah, 2Ah   ;Then we call int 21h,2Ah, which will give 
 int 21h   ;us the current date 
 
 lea di, date_str  ;Then we load the address of the date_str string 
 add di, 17   ;and set si to point at the first y in yyyy-... 
 
 
 mov ax, cx   ;Next we mov cx to ax and 
 call todec   ;call todec 
 inc di   ;We skip the '-' character... 
 
 xor ax, ax   ;Then we empty ax 
 mov al, dh   ;And set the low-byte of ax to dh 
 call todec 
 inc di   ;Skip character in string... 
 
 xor ax, ax   ;Empty ax 
 mov al, dl   ;Set low-byte to dl 
 call todec   ;Convert it to base10 
 
 
 lea di, time_str  ;Now we load the time_str string 
 add di, 17   ;And set the correct pointer offset 
 
 mov ah, 2Ch   ;And then we call int 21h,2Ch 
 int 21h   ;which will give us the current time 
 
 
 xor ax, ax   ;Empty ax 
 mov al, ch   ;Set low-byte to ch 
 call todec   ;Convert it to base10 
 inc di   ;Skip character 
 
 mov al, cl   ;Set low-byte to cl 
 call todec   ;Convert to base10 
 inc di   ;Skip character 
 
 mov al, dh   ;Set low-byte to dh 
 call todec   ;Convert to base10 
 inc di   ;Skip character 
 
 mov al, dl   ;Set low-byte to dl 
 call todec   ;Convert to base10 
 
 
 mov dx, offset date_str ;Now load offset of the date_str string into dx 
 call print   ;And print the (modified) string 
 
 mov dx, offset time_str ;Load offset of the time_str string into dx 
 call print   ;And print the (modified) string 
 
 mov ax, 4C00h  ;Do a clean exit(error code=00) 
 int 21h 
 
  
 
;=================================================================== 
;todec - converts the contents of ax into base10 ascii character(s) 
;        of length bx 
;        min_size defines minimum length of result, and padd_char 
;        defines the padding character. 
;The result(s) are stored at ds:di 
;=================================================================== 
todec proc 
 push ax   ;Save all registers 
 push bx 
 push cx 
 push dx 
 
 
 xor cx,cx   ;Empty the POP counter 
 mov bx,10   ;Base divisor 
decloop:   
 xor dx,dx   ;Set the high 16-bits to 0 
 div bx   ;Preform division(dx=remainder, ax=quotient) 
 inc cx   ;Increase the counter 
 push dx   ;and save the remainder 
 cmp ax,0   ;If the quotient != 0  
 jnz decloop   ;then get one more number 
 
 
 mov bx, [min_size]  ;Load min_size value into bx 
 mov dl, [padd_chr]  ;Load padd_chr value into dl 
padd_result: 
 cmp cx, bx   ;Is cx >= min_size? 
 jge poploop   ;If so, proceed 
 
 mov byte ptr ds:[di], dl ;Else padd with padd_chr 
 inc di   ;and increase string pointer 
 dec bx   ;decrease bx 
 jmp padd_result  ;and test for more padding 
  
 
poploop: 
 pop dx   ;Get the number of the stack 
 add dl,'0'   ;and add '0' to it 
 mov byte ptr ds:[di], dl ;Modify the string at ds:di 
 inc di   ;Increase the string pointer 
 dec cx   ;Decrease the loop counter 
 jnz poploop  
 
 
 pop dx   ;Restore all registers 
 pop cx 
 pop bx 
 pop ax 
 ret    ;And return from call 
todec endp 
 
 
;=========================================================== 
;print - prints the string pointed to by dx using int 21h,09 
;=========================================================== 
print proc 
 push ax   ;Save ax 
 push ds   ;and ds onto the stack 
 
 mov ax, @data  ;Then get the address of the data segment 
 mov ds, ax   ;and store it into ds 
 mov ax, 0900h   
 int 21h   ;and then print the message pointed to by dx 
  
 pop ds   ;Retrieve ds 
 pop ax   ;and ax from stack 
 
 ret 
print endp 
 
main endp 
end main