x86 assembly count numbers

Started by
4 comments, last by carter001 17 years, 3 months ago

 .model small
 .stack 100h
 .data
.code
	

        mov ah, 02h     	; DOS function call 2, character output
	mov dl, 41h      	; the character 041h (i.e.'A')
        mov cx, 26              ; start the counter at 26 in cx
again:	int 21h         	; print character
	inc dl                	; the next character into dl
	loop again  	        ; subtract 1 from cx, if not zero jump back to again

finish:      	mov ax, 4C00h 	; Terminate program
	              int 21h   ; correctly


end
The above prints A-Z,I'm trying to change the code so it will print 1 to 100 instead but my attempts have failed.. Any suggestions would be appreciated.. thanks.
Advertisement
This snippet from the ascii chart might help.

Dec  	Oct  	Hex  	Character  	48  	60  	30  	0  	49 	61 	31 	1 	50 	62 	32 	2 	51 	63 	33 	3 	52 	64 	34 	4 	53 	65 	35 	5 	54 	66 	36 	6 	55 	67 	37 	7 	56 	70 	38 	8 	57 	71 	39 	9 
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Printing 1-100 is a more complex problem, since you need to print more than one character per number.

Changing the code to print 1-9 rather than A-Z should be straightforward. After that, you'll need to write a function to call to print your number, rather than simply calling DOS function 2 directly.
You have to convert the integer number to a string. For instance, the number 100 is represented by 3 characters '1', '0' and '0'.

Conversion of a decimal number to a string of characters can be implemented in C/C++ like this:
void print(unsigned int i, unsigned int base){	static char lut[] = "0123456789abcdef";	unsigned int r;	char c;	do	{		// calculate remainder		r = i % base;		// convert to character		c = lut[r];		// do something with c		print(c);		// cut off last digit		i /= base;	}	while (i);}


Similar code can be written in assembler:
mov bx, 10     ; base is decimal 10mov ax, 4711   ; ax stores i, for now its just 4711next_digit:mov dx, 0      ; dx:ax stores idiv bx         ; calculate remainder r in dx and quotient in axadd dx, 30h    ; 30h is '0'push ax        ; remember axmov ah, 02h    ; DOS function callint 21h        ; call DOSpop ax         ; restore quotientor ax, ax      ; test if ax is already zerojne next_digit ; repeat while i is not 0

Note that instead of using a lut, the value of 30h is added to the remainder. So no hexadecimal numbers can be printed this way. You will also have to implement a way to reverse the digit sequence (for instance by a recursive call or by writing the digits backwards to an array, then using function 09h instead).
Thanks for the feedback,yea I can print 1 to 9 fine

.model small
.stack 100h
.data
.code


mov ah, 02h ; DOS function call 2, character output
mov dl, 31h ; the character 031h (i.e.'1')
mov cx, 9 ; start the counter at 9 in cx
again: int 21h ; print character
inc dl ; the next character into dl
loop again ; subtract 1 from cx, if not zero jump back to again

finish: mov ax, 4C00h ; Terminate program
int 21h ; correctly


end

but once it goes past 9 it prints the next ascii character,obviously
1,2,3,4,5,6,7,8,9,:,; etc etc

Could I use a variable to add 1 ,1+1+2+1+3+1+4+ and so on within the loop?

sorry nmi I just read your post,Thanks for your help..I'll study it

This topic is closed to new replies.

Advertisement