Having completed the code for that, I hope posting it here will help those who are looking for away to do it :)
The code is not optimized, nevertheless, so you should use it as a reference only.
I made use of the 21, 9 interrupt, which prints a string delimited by the '$' to the screen.
;count the number of digit of a 1-byte integer
;number stored in AX (AL actually, since it's 1-byte)
;count value stored in CX
;convert the number into a string and print out
myStackSeg segment stack
db 128 dup (?) ;space for stack
tos label word ;top of stack
myStackSeg ends
myDataSeg segment
_dec_10 db 10 ;declare decimal value 10
intStr db 4 dup(?) ;1-byte integer -> 3 digit max -> 4 space (1 for '$')
myNumber equ 176
_eos db '$'
myDataSeg ends
myCodeSeg segment
assume CS:myCodeSeg, DS:myDataSeg, SS: myStackSeg
start:
;initialize segment register
mov AX, seg myDataSeg ;
mov DS, AX ;initialize DS
mov AX, seg myStackSeg ;
mov SS, AX ;
mov SP, offset tos ;initialize SS
;convert the digits into string
mov DX, myNumber
mov BX, offset intStr
;count the number of digit of a number in AX
mov AX, DX ;AX store the number
xor CX, CX ;clear CX
jmp countDigit ;start counting
nextDigit: ;still got digit remain
mov AH, 00h ;clear remainder
jmp countDigit ;count again
countDigit: ;count and div 10
div _dec_10 ;div 10; AL=remainder, AH=div result
inc CX ;at least 1 digit
cmp AL, 0 ;if (div result)!=0
jne nextDigit ;prepare to count again
;CX hold no of digit
mov DL, CL ;
add DL, 48 ;convert to corresponding ASCII character
mov AH, 02h ;print character to screen
int 21h ;
mov DI, BX ;DI=@intPtr (array storing the characters to print)
add DI, CX ;move DI to end of string (eos)
mov AH, _eos ;copy the string delimiter '$'
mov [DI], AH ;to then end of string
mov AX, myNumber ;AX=number to print
;jmp convertDigit ;start printing
convertDigit:
div _dec_10 ;div 10 AX
add AH, 48 ;AH=AX mod 10. convert it to ASCII character
mov [DI], AH ;copy the character to its position in the output str
mov AH, 00h ;clear the character -> AX=AX div 10
dec DI ;move back 1 space in the output str
loop convertDigit ;loop till all digits are converted
mov DX, BX ;print the converted string
mov AH, 09h ;
int 21h ;
exit:
mov AH, 4Ch ;exit
int 21h ;
myCodeSeg ends
end start
No comments:
Post a Comment