0

I have been working on project with DOS Debug. I have to take string and count symbol's 0, 2, 3 and 6 bit sum. If it is even, I need to turn that symbol into 4. And I have to print out string backwards. I have done almost everything.

But I have a problem and I stuck. It only prints out 12 characters. Where the problem could be?

Input: abcdefghijklmn

Output: n4lk4i4g4e44

Expected output: n4lk4i4g4e44b4 (more, than 12 characters)

This is the code

a800
db 50 00

a100
mov ah, 09
mov dx, 700
int 21
mov ah, 3f
mov dx, 800
int 21

a150
mov bx, 800
mov si, 0
jmp 250

a250
mov al, [bx+si]
mov cl, [bx+si]
mov byte [750], cl
cmp al, 20
jl 490
mov ah, 0
clc
rcr al, 1
adc ah, 0
clc
rcr al, 2
adc ah, 0
clc
rcr al, 1
adc ah, 0
clc
rcr al, 4
adc ah, 0
mov al, ah
mov ah, 0
clc
rcr al, 1
adc ah, 0
cmp ah, 0
jg 300
jmp 320

a300
mov byte [bx+si], 34
inc si
jmp 250

a320
mov byte [bx+si], [750]
inc si
jmp 250

a490
mov byte [bx+si+1], 24
xor dx, dx
jmp 500

a500
mov ah, 02
mov dl, [bx+si]
int 21
dec si
loop 500
mov ah, 4c
int 21

a700
db "Iveskite teksta " 0A 0D "$"

n bb.com
r cx
1000
w
q

a250 counts bit's sum

a490 adds $ sign to the end

EDIT: I think that loop stops working after twelve reps.

Kurbamit
  • 89
  • 8
  • 1
    `mov byte [bx+si], [750]` <-- What is that supposed to do? Did you mean to write `mov byte [bx+si], 750`? (which would be strange since 750 doesn't fit in a byte). – Michael Oct 24 '22 at 12:58
  • 2
    int21/3f expects a count in `cx` (and also a file handle in `bx` which you accidentally seem to have working but should set explicitly). – Jester Oct 24 '22 at 13:02
  • 1
    Your description is also a bit vague. Please show the input you're testing with, as well as the expected and actual outputs. – Michael Oct 24 '22 at 13:03
  • Oh, that is my bad. I put one char in [750], so I could put that back in it's place. I fixed that one with putting [750] in register, and then in it's place. Thank you – Kurbamit Oct 24 '22 at 13:05
  • I updated the info about input and output. – Kurbamit Oct 24 '22 at 13:10
  • Where do you set up the loop count (i.e. `cx`) for `loop 500` ? – Michael Oct 24 '22 at 13:11
  • I tried to do that with SI. I am using SI as a counter. It counts, how many characters there are. – Kurbamit Oct 24 '22 at 13:14
  • 1
    The `loop` instruction has no idea that you're using `si` as a counter. It always uses `cx` (or `ecx` or `rcx`). – Michael Oct 24 '22 at 13:33
  • In the `a100` chunk after the second `int 21` you are missing a jump. – ecm Oct 24 '22 at 13:57

1 Answers1

0

The answer is simpler than expected. Thanks to @Jester and @Michael comments. I had a bad loop. I just moved si to cx and it works perfectly. Thank you all for help.

a490
mov byte [bx+si+1], 24
xor cx, cx
mov cx, si
inc cx
jmp 500

a500
mov ah, 02
mov dl, [bx+si]
int 21
dec si
loop 500
mov ah, 4c
int 21
Kurbamit
  • 89
  • 8
  • 2
    You don't need to zero CX with XOR before `mov cx, si`. `mov` overwrites the destination regardless of the old value. Also, this is still really over-complicated; you're already counting down `si` toward zero, just use it as the loop counter instead of the `loop` instruction. No need to use extra instructions for CX. e.g. `dec si` / `jge 500` like the bottom of a `do{}while(--si >= 0)`. (Using `jge` instead of `jnz` solves the off-by-one problem you're avoiding here with `inc cx`.) – Peter Cordes Oct 24 '22 at 13:47
  • 2
    Only use the `loop` instruction when it actually *saves* instructions, as a code-size optimization. And then only if you don't care about speed on modern Intel CPUs: [Why is the loop instruction slow? Couldn't Intel have implemented it efficiently?](https://stackoverflow.com/q/35742570) – Peter Cordes Oct 24 '22 at 13:48