Reputation: 25
I am trying to write a program in assembly (masm) that will accept a user input and the program will output a count starting from 0 up to that input (between 0 to 99). The error I am encountering is that no matter what number I enter, the program loops to what seems to be a random number and then terminates. Any help would be much appreciated!
cseg segment 'code'
assume cs:cseg, ds:cseg, ss:cseg, es:cseg
org 100h
start:
mov ah, 09
mov dx, offset Intro
int 21h
mov ah, 01
int 21h
sub al, 30h
mov bl,0Ah
mul bl
mov bl, al
mov ah, 01
int 21h
sub al, 30h
add bl, al
mov cl, bl
jmp again
again:
mov ah, 09
mov dx, offset Hello
int 21h
mov dx, offset msg1
int 21h
mov dx, offset msg
int 21h
inc byte ptr msg
mov al, msg
cmp al, 3ah
jg reset
loopne again
reset:mov byte ptr msg, 30h
inc byte ptr msg1
cmp cx, 0
jg again
jmp done
done:
mov ah, 4ch
int 21h
org 200h
Intro db "Please input how many times you would like the loop to run." , 20h, 20h, "$"
Hello db "hello world" , 20h, 20h, "$"
msg db 30h, 13, 10, "$"
msg1 db 30h, "$"
cseg ends
end start
Upvotes: 2
Views: 113
Reputation: 39676
The loopne
instruction uses the CX
register and you only initialized the CL
register which is the low byte of CX
. If you insist on using loopne
, clear CH
which is the high byte of CX
.
Of course the solution is much simpler.
Keep the logic of modifying the textual counter independant of the logic of the main loop. The worst that will happen is that the textual counter will have been incremented once for nothing.
Also there's no benefit in moving the user inputted value in CX
. You can just as well or even better work with it from BL
.
add bl, al ;BL has user input 00..99
again:
mov ah, 09
mov dx, offset Hello
int 21h
mov dx, offset msg1
int 21h
mov dx, offset msg
int 21h
; This is the textual counter update
inc byte ptr msg ;Raise the ones
mov al, msg
cmp al, "9"
jbe SameDecade
mov byte ptr msg, "0" ;Reset the ones
inc byte ptr msg1 ;Raise the tens
SameDecade:
; This is the main loop counter update
sub bl, 1
jnb again
mov ax, 4C00h ;DOS.TerminateWithExitcode
int 21h
Upvotes: 1
Reputation: 24229
two problems:
you're assigning bl
to cl
, without clearing ch
part, its better to use something like movzx cx, bl
you're not changing cx, before comparison, add dec cx
before cmp cx, 0
just advice: you do not need to use jmp again
or jmp done
Upvotes: 0