0

I was tasked to do a code that would output the equal amount of 'Text Here' depending on the user input. However, I seem to get bewildered by my cmp function.

;Get keyboard input
mov ah, 01h 
int 21h

;Save to bl for later use
mov bl, al
jmp isa

isa:
mov ah, 09h 
mov dx, offset text 
int 21h

cmp bl, bl
jne isa
je exit

What I get with this code is only one output of 'Text' no matter what number I input.

EDIT: I tried this but now my output is infinite :(

isa:
inc bl
mov ah, 09h 
mov dx, offset ulit 
int 21h


cmp bl, 30h
jne isa
je exit
Sifeng
  • 1
  • 1
  • As written, this should be an infinite loop, because you never decrement `BL`. What is `text`? Is it terminated with a dollar sign as the DOS API expects? – Cody Gray - on strike Mar 23 '19 at 04:43
  • @CodyGray: This is `cmp`, not `test`. It's checking if `bl` is equal to itself, like a `sub same,same` zeroing idiom. Based on the title, this is maybe a duplicate of my Q&A about looping ([Why are loops always compiled into "do...while" style (tail jump)?](//stackoverflow.com/q/47783926)), but the question body is more like how to use `cmp`. But IDK what they want as an exit condition, so close as unclear would be reasonable. – Peter Cordes Mar 23 '19 at 04:48
  • Of course it is. The eyes see what they want to see. Not so sure about that duplicate marking, though, @Peter. I assume you were marking it as a canonical "how do I write a loop in asm?" question, but I don't think your answer there really shows that. – Cody Gray - on strike Mar 23 '19 at 04:53
  • @CodyGray: yeah, you're right. It has some example loops, but all the text assumes the reader already understands the fundamentals of how loops work. I don't recall any particular tutorial Q&A about how to loop, but I'm sure there have been *many* answers that explain it in passing. :/ – Peter Cordes Mar 23 '19 at 04:57
  • @CodyGray Yes I'm sorry I forgot about the $ sign. 'Text' simply any is any text we would like to be looped. – Sifeng Mar 23 '19 at 10:37
  • I am sorry but the thread you suggested is too hard for me to understand. I'm just to this stuff and I hope for a simpler way :/ – Sifeng Mar 23 '19 at 10:45
  • Have you tried single-stepping your code with a debugger? *Now* your code has the problem Cody pointed out: You don't modify `bl` inside the loop, so the comparison is the same every time. – Peter Cordes Mar 23 '19 at 14:19

1 Answers1

0

First of all, make sure that you initialize the BX register to zero before you start your loop:

...
xor bx,bx
isa:
...

To avoid the DOS interrupt to overwrite the contents of your (used) BX register, secure it on the stack (not sure about the calling conventions of DOS interrupts, was too long ago for me):

...
push bx
int 21h
pop bx
Martin
  • 116
  • 2