WEB开发网
开发学院软件开发汇编语言 (汇编源代码 )获取当前系统时间 阅读

(汇编源代码 )获取当前系统时间

 2008-04-29 09:34:38 来源:WEB开发网   
核心提示:;==;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,

;==========================================
;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_strdb"Current date is: yyyy-mm-dd", 0Ah, 0Dh, "$"
time_strdb"Current time is: hh.mm.ss:xx", 0Ah, 0Dh, "$"
min_sizedw?
padd_chrdb ?
;===========================
;Code segment starts here
;===========================
.code
mainproc
movax, seg @data ;First we get the data segment address
movds, 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
  movah, 2Ah ;Then we call int 21h,2Ah, which will give
int21h ;us the current date
  leadi, date_str ;Then we load the address of the date_str string
adddi, 17 ;and set si to point at the first y in yyyy-...
movax, cx ;Next we mov cx to ax and
calltodec ;call todec
incdi ;We skip the '-' character...
  xorax, ax ;Then we empty ax
moval, dh ;And set the low-byte of ax to dh
calltodec
incdi ;Skip character in string...
  xorax, ax ;Empty ax
moval, dl ;Set low-byte to dl
calltodec ;Convert it to base10
leadi, time_str ;Now we load the time_str string
adddi, 17 ;And set the correct pointer offset
  movah, 2Ch ;And then we call int 21h,2Ch
int21h ;which will give us the current time
xorax, ax ;Empty ax
moval, ch ;Set low-byte to ch
calltodec ;Convert it to base10
incdi ;Skip character
  moval, cl ;Set low-byte to cl
calltodec ;Convert to base10
incdi ;Skip character
  moval, dh ;Set low-byte to dh
calltodec ;Convert to base10
incdi ;Skip character
  moval, dl ;Set low-byte to dl
calltodec ;Convert to base10
movdx, offset date_str;Now load offset of the date_str string into dx
callprint ;And print the (modified) string
  movdx, offset time_str;Load offset of the time_str string into dx
callprint ;And print the (modified) string
  movax, 4C00h ;Do a clean exit(error code=00)
int21h
  ;===================================================================
;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
;===================================================================
todecproc
pushax ;Save all registers
pushbx
pushcx
pushdx
xorcx,cx ;Empty the POP counter
movbx,10 ;Base divisor
decloop: 
xordx,dx ;Set the high 16-bits to 0
divbx ;Preform division(dx=remainder, ax=quotient)
inccx ;Increase the counter
pushdx ;and save the remainder
cmpax,0 ;If the quotient != 0
jnzdecloop ;then get one more number
movbx, [min_size] ;Load min_size value into bx
movdl, [padd_chr] ;Load padd_chr value into dl
padd_result:
cmpcx, bx ;Is cx >= min_size?
jgepoploop ;If so, proceed
  movbyte ptr ds:[di], dl;Else padd with padd_chr
incdi ;and increase string pointer
decbx ;decrease bx
jmppadd_result ;and test for more padding
  poploop:
popdx ;Get the number of the stack
adddl,'0' ;and add '0' to it
movbyte ptr ds:[di], dl;Modify the string at ds:di
incdi ;Increase the string pointer
deccx ;Decrease the loop counter
jnzpoploop
popdx ;Restore all registers
popcx
popbx
popax
ret  ;And return from call
todecendp
;===========================================================
;print - prints the string pointed to by dx using int 21h,09
;===========================================================
printproc
pushax ;Save ax
pushds ;and ds onto the stack
  movax, @data ;Then get the address of the data segment
movds, ax ;and store it into ds
movax, 0900h 
int21h ;and then print the message pointed to by dx
popds ;Retrieve ds
popax ;and ax from stack
  ret
printendp
  mainendp
endmain

Tags:汇编 源代码 获取

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接