成都星海电玩:汇编语言的子程序的应用并且有从键盘输入的例子,

来源:百度文库 编辑:神马品牌网 时间:2024/05/02 19:19:46
本人初学汇编,请各位高手援助,谢谢

;==========================================
;A little assembly app that shows the current date and time.
;==========================================

.model small
.stack 256(0)

;===========================
;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
Start:
MOV AX, SEG @data ;First we get the data segment address
MOV DS, AX ;and store it into ds

MOV [min_size], 2 ;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

;===================================================================
; converts the contents of ax into base10 ascii characters
; 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

END start

老实说,我也是初学,这个程序我自己都没看明白,不过好像可以用的样子,呵呵~