0

I don't understand why this code print me the char I've insert back and the alphabet string if nobody told him to do it...

.model
.stack 100h
.data

lettere DB 10 DUP(?)
frase1 DB 10,13,"inserisci 10 caratteri$"
frase2 DB ">>$"
caporiga DB 10,13,"$"
counter DB 0
char DB ?

alpha DB "abcdefghijklmnopqrstuvwxyz$"

.code
.startup

  lea dx,frase1
  mov ah,09h
  int 21h

  lea dx,caporiga
  mov ah,09h
  int 21h

insertLoop:

  inc counter

  lea dx,counter
  mov ah,02h
  int 21h

  lea dx,frase2
  mov ah,09h
  int 21h

  mov ah,01h
  int 21h
  mov lettere+(counter-1),al

  lea dx,caporiga
  mov ah,09h
  int 21h

  cmp counter,10
  JB insertLoop

  mov ah,4ch
  int 21h
end
ndim
  • 35,870
  • 12
  • 47
  • 57
CRL
  • 27
  • 3
  • 1
    Could you please phrase this in a better way? I'm having trouble understanding your question. Also please make sure to place your code in between triple backticks like so: \`\`\`code\`\`\` – Sir Archibald Humphrey Mar 30 '22 at 22:11
  • 1
    `char` is just before the alphabet, and I'm guessing you are printing using `09h` which expects `$` at the end. Your `char` has only the characters and misses that and it just goes over printing the alphabet until it find `$` at the end of it. – Paweł Łukasik Mar 30 '22 at 22:16
  • I'm surprised `mov lettere+(counter-1),al` assembles at all. It definitely can't use the `db` at `counter` as an index for your array; probably it's just adding the two addresses together if your assembler doesn't reject that as being nonsense like it should. Use a debugger to single-step, and look at disassembly of the resulting machine code. – Peter Cordes Mar 30 '22 at 22:16
  • 1
    To index memory, you need the index in a register (BX, SI, or DI), like `[lettere + SI]`. Use registers for this, not memory, that's what they're for. – Peter Cordes Mar 30 '22 at 23:24
  • _"if your assembler doesn't reject that as being nonsense like it should"_ In MASM syntax brackets are sometimes optional, so that instruction ought to be equivalent to `mov [lettere+counter-1],al`. I don't see why the assembler should reject that, unless maybe if the address calculation overflows. – Michael Mar 31 '22 at 06:16
  • @Michael: When does it ever make sense to add the addresses of two symbols? Maybe if you've defined one of them with an `equ` or something instead of a real address? Interestingly, NASM *does* accept `mov eax, foo+bar` (as a mov-immediate with just offsets relative to the start of the file or section, not absolute addresses: https://godbolt.org/z/zsGxxYnhY), but `mov eax, [foo+bar]` is rejected with "invalid effective address: impossible segment base multiplier", when assembling into an ELF object of flat binary. YASM rejects even the immediate. – Peter Cordes Apr 04 '22 at 01:34

1 Answers1

2

I don't understand why this code print me the char I've insert back and the alphabet string if nobody told him to do it...

The mov lettere+(counter-1),al instruction does not do what you want.
MASM will add the (offset)addresses of lettere (0) and counter (41), and then subtract 1, yielding the address 40 where the instruction then overwrites an important $ string terminator. The next time that your program tries to print a newline caporiga, the DOS.PrintString function 09h will continue printing characters until it finds the $ that ends your alphabet string.


lea dx,counter
mov ah,02h
int 21h

This is another error. The DOS.PrintCharacter function 02h expects to find a character in the DL register, but this lea dx,counter instruction clearly loads an address.

mov  dl, counter
add  dl, '0'
mov  ah, 02h
int  21h

The correct code first retrieves the count [1,10] from memory and then adds '0' (48) in order to convert into a printable character. This will of course fail when your count is 10. Look in Displaying numbers with DOS to learn how to overcome this problem.


One possible solution:

...

  xor BX, BX        ; Reset counter
insertLoop:
  inc BX            ; Increment counter
  lea dx, [BX + 48] ; -> DL = "1", "2", ...
  mov ah, 02h       ; Won't work when counter is 10
  int 21h

  lea dx, frase2
  mov ah, 09h
  int 21h

  mov ah, 01h
  int 21h
  mov [lettere + BX - 1], al

  lea dx, caporiga
  mov ah, 09h
  int 21h

  cmp BX, 9
  jb  insertLoop

...
Sep Roland
  • 33,889
  • 7
  • 43
  • 76