-3

I need to write names from keyboard and then display them, one on each line. They should be displayed with an index before of them. For example if I write the names elena and maria the should be displayed as 1.elena 2.maria

I tried adding a counter variable but I have some errors when I try to run the program in DosBox. Can someone help me? Here is my clean label that is outputting the names:

 lista:
  mov  dx, offset nume
print_names:
push dx
  mov  dl, 13          ; carriage return
  mov  ah, 02h
  int  21h
  mov  dl, 10          ; linefeed
  mov  ah, 02h
  int  21h
  pop  dx
  mov dx, offset index
  mov ah, 9
  int 21h
  inc byte ptr index
  mov  dx, offset nume
  mov  ah, 09h
  int  21h
  ; (*)
  add  dx, 5 + 1
  cmp  dx, numePointer ; check if the current name is the last one
  jb   print_names
  jmp  bucla           ; return to main loop
Andrei2308
  • 23
  • 5
  • 1
    What errors? A [mcve] needs details like exactly what errors you get. – Peter Cordes Jan 16 '23 at 15:49
  • I have "Illegal use of register" type of errors. The code I presented just prints the name as they are without any index. I was wondering how I could modify it correctly to add index . – Andrei2308 Jan 16 '23 at 16:21
  • 1
    You still haven't quoted the exact error message, or which line it's happening on. [edit] your question with that information. All the instructions in the code you posted look valid to me, but maybe I'm overlooking something because you haven't told us where to look. – Peter Cordes Jan 16 '23 at 16:22
  • This is the label that displays the name without indexes. So basically this is before I tried addind indexes to the outputs and I thought maybe someone will tell me what I need do to. I think the idea that i started with, on adding indexes is bad, but should I post the label code that gives the errors? – Andrei2308 Jan 16 '23 at 16:31
  • This code is correct, but do show us what you have tried concerning adding the numerals. – Sep Roland Jan 16 '23 at 17:56
  • In that code, this is my method to show indexes, but mov dl, '0' + bx / 10 and mov dl, '0' + bx % 10 are not permitted – Andrei2308 Jan 16 '23 at 18:05
  • 1
    Right. Those are calculations that need to be done runtime using `div` or you can just code your counter in ascii to start with. – Jester Jan 16 '23 at 18:39
  • I tried declaring an index counter, but I have the error illegal instruction on int21h in the code mov dx, offset index // mov ah, 09h // int 21h. I updated the code for you to see – Andrei2308 Jan 16 '23 at 18:44
  • punct is the following : punct db '.' ,10, '$' to just print '.' after the index – Andrei2308 Jan 16 '23 at 18:45
  • That's just a typo, you need a space so `int 21h` not `int21h`. – Jester Jan 16 '23 at 18:45
  • I saw and modified, but still no index is displayed when I output – Andrei2308 Jan 16 '23 at 18:49
  • I realised that the index and . will be shown but a newline will be printed so I updated my code again – Andrei2308 Jan 16 '23 at 18:51
  • Do not put the `afisareindex` procedure in the middle of your main code. So where are we now in terms of results? :D – Jester Jan 16 '23 at 19:01
  • I made some progress ( see the updated code ), but now when I display the names, they are just looping infinitely. – Andrei2308 Jan 16 '23 at 19:09
  • See my answer! Your reload of `offset nume` is wrong. That's the infinite loop you get. – Sep Roland Jan 16 '23 at 19:12

1 Answers1

1

The addition is destroying the DX pointer for the name to be displayed!
Why did you put the additional code between proc endp?
punct db '.' ,10, '$' does not need the 10. Should be punct db '.$' or even better, combine it with index like in index db '?.$'.
And still better, combine the newline with it too:

lista:
  mov  dx, offset nume
print_names:
  push dx                        ; (1)
  mov  dx, offset numeIndex
  mov  ah, 09h
  int  21h
  inc  byte ptr [numeIndex + 2]  ; "1" -> "2" -> "3" ...
  pop  dx                        ; (1)

  mov  ah, 09h
  int  21h
  add  dx, 5 + 1
  cmp  dx, numePointer ; check if the current name is the last one
  jb   print_names
  jmp  bucla           ; return to main loop

  ...

numeIndex db 13, 10, 49, 46, 36

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • I modified my code as you said and declared index db '?.$' and the indexes are shown but now the indexes are special characters not 1,2,3 – Andrei2308 Jan 16 '23 at 19:14
  • The **?** sign if for you to replace by "0" or "1" depending on how you want the numbering to start. – Sep Roland Jan 16 '23 at 19:16
  • I replaced it with 1 : index db '1$' – Andrei2308 Jan 16 '23 at 19:17
  • That's fine *if* you place `inc index` after displaying the index. I will update my answer. – Sep Roland Jan 16 '23 at 19:18
  • I can see that it is correct, but idk why it doesn t show the indexes correct. Do you mind if I send you the full code so you try it? Maybe it has something to do with my DosBox – Andrei2308 Jan 16 '23 at 19:21
  • On the first name it prints me a smiley face instead of one and on the second it prints me a heart – Andrei2308 Jan 16 '23 at 19:24
  • Then TASM is considering your 1 as **number 1** instead of **character "1"**. Try the new code `index db 13, 10, '1.$'` Perhaps change to double quotation marks: `index db 13, 10, "1.$"` – Sep Roland Jan 16 '23 at 19:29
  • I still have the same problem. The TASM must be reading the number 1 as something else, reffering to a special character. I tried putting 48 which is the ascii code for 1 but it didn t work either – Andrei2308 Jan 16 '23 at 19:32
  • It is strange that no matter what number I write inside the " " when I declare the index, the indexes outputs are the same. One smiley face and one heart for the first two lines – Andrei2308 Jan 16 '23 at 19:34
  • Are you using the code from my latest edit to the answer? Is *index* placed in the same data section where you have *nume*? You could try `index db 13, 10, 49, 46, 36` – Sep Roland Jan 16 '23 at 19:35
  • I am using the code. This program gives me some headaches for 5 days now. If I write maria and elena as input names the output will be smileyface //nextline 1.maria(smileyface) //nextline 2.elena(smileyface). – Andrei2308 Jan 16 '23 at 19:42
  • Just updated my answer one more time. If that still doesn't work, then I suggest you post your complete program so I can take a look. Either make it a new post or add it to the current post *but don't overwrite what the current post contains!*. – Sep Roland Jan 16 '23 at 19:46
  • 1
    Bro you are a legend, it works now. I rember you helped me yesterday too when i had blocked and didn't know what to do. Now I hope I can finish my code. The last thing that I need to do is to delete a name from the list by its index. – Andrei2308 Jan 16 '23 at 19:58