Reputation: 53
I need to create an assembly program that prints: 0 01 012 0123 ... 0123456789 It seems like I need to create a variable that I can increment that stops the code once I get to it so I can go back and start again, this is my attempt so far. I can already get it to print 0123456789 ten times but it's getting it to count up is the problem. Here is that code
.MODEL MEDIUM
.STACK
.DATA
number DB 1
.CODE
.STARTUP
mov bl,10
nextline: mov dl,'0' ; '0' is ASCII 48
nextchar: mov ah,02h ; print ASCII char in dl
int 021h
inc dl
cmp dl,':';‘:' is ASCII for 10
jnz nextchar
push dx ; save value of dl and dh on stack
mov ah,02h ; print ASCII char in dl
mov dl,13 ; carriage return (move to start of line)
int 021h
mov dl,10 ; line feed (next line)
int 021h
pop dx ; restore value in dl (and dh)
dec bl
jnz nextline
.EXIT
END
And here is my attempt at making it count up, perhaps I don't fully understand how variables work yet
.MODEL MEDIUM
.STACK
.DATA
number DB '1' ;changed from 1 to '1'
.CODE
.STARTUP
mov bl,10
nextline: mov dl,'0' ; '0' is ASCII 48
nextchar: mov ah,02h ; print ASCII char in dl
int 021h
inc dl
cmp dl,number;‘:' is ASCII for 10
jnz nextchar
push dx ; save value of dl and dh on stack
mov ah,02h ; print ASCII char in dl
mov dl,13 ; carriage return (move to start of line)
int 021h
mov dl,10 ; line feed (next line)
int 021h
pop dx ; restore value in dl (and dh)
dec bl
add number,1 ;moved to outer loop
jnz nextline
.EXIT
END
Upvotes: 4
Views: 6628
Reputation: 9899
First off, do yourself a favour and change the name of your variable from number to character since that's what it is now that you've followed the advice by others.
With the help of Michael you've already fixed a few things to your code. What's still posing problems is next part of the code:
add character,1 ;moved to outer loop jnz nextline
As written, the outer loop will stop when the byte-sized character increments from the value 255 to 0.
You need to limit the raising of character; up to a maximum of '9'.
You can use the cmp
instruction for that:
add character, 1
cmp character, '9'
jbe nextline
The jbe
instruction will keep jumping back (looping) for as long as the character is below or equal to '9'.
It also avoids using ugly constructs like:
‘:' is ASCII for 10
There's no more need for a counter in BL
and preserving DX
around the newline part is redundant.
This is your revised code:
.MODEL MEDIUM
.STACK
.DATA
character DB '0'
.CODE
.STARTUP
mov ah, 02h ; print ASCII char in dl
nextline:
mov dl, '0'
nextchar:
int 21h
inc dl
cmp dl, character
jbe nextchar
mov dl, 13 ; carriage return
int 21h
mov dl, 10 ; line feed
int 21h
add character, 1
cmp character, '9'
jbe nextline
.EXIT
END
Take note that character now starts at '0' (and not '1').
Upvotes: 2