Jump to content
  • Advertisement
Sign in to follow this  
carter001

x86 assembly count numbers

This topic is 4138 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

 .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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 10
mov ax, 4711 ; ax stores i, for now its just 4711

next_digit:
mov dx, 0 ; dx:ax stores i
div bx ; calculate remainder r in dx and quotient in ax
add dx, 30h ; 30h is '0'
push ax ; remember ax
mov ah, 02h ; DOS function call
int 21h ; call DOS
pop ax ; restore quotient
or ax, ax ; test if ax is already zero
jne 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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!