0

Below i wrote a code that will compare characters and move letters to litere array and numbers to numere array. The problem is that when i am trying to print the letters array using printf , it actually not printing anything. I think i made a mistake when i was moving values from the array that have all the characters to another array, but i am not sure.

      litere times 255 db 0      
      numere times 255 db '?'


     mov ESI,0 ;index of the full array of characters from text file
                            mov EDI,0 ;index of numbers array
                            mov EBX,0 ;index of letters array
                            mov ECX,[numar_caractere_citite] ;the number of all numbers and letters from the original array


   repeta:
                        push ECX
                               mov DL,byte[continut_fisier+esi]
                                    cmp DL,"0"
                                    jl cmp_litere
                                    cmp DL,"9"
                                    jg cmp_litere     ;if EDX>=0 && EDX <= 9 --> we found a number
                                    jmp found_cifra
                                    cmp_litere:
                                    cmp DL, "A"               ; compare EDX with "A"
                                    jl continua               ; jump to next character if less
                                    cmp DL, "Z"               ; compare EDX with "Z"
                                    jle found_letter           ; if EDX is >= "A" && <= "Z" -> found a letter
                                    cmp DL, "a"               ; compare EDX with "a"
                                    jl continua                ; jump to next character if less (since it's between "Z" & "a")
                                    cmp DL, "z"               ; compare EDX with "z"
                                    jg continua              ; above "Z" -> not a character
                                    found_letter:
                                       mov [litere+EBX],byte DL
                                       inc EBX
                                       jmp continua
                                    found_cifra :
                                       mov [numere+EDI],byte DL                          
                                       inc EDI
                               continua:inc ESI
                                        pop ECX
                         loop repeta

;HERE i am trying to print each character from litere array

            mov ECX,[dim_sir_litere] ;number of letters from litere array
                                mov ESI,0
                                afisare_litere: 
                                           push ECX 
                                           mov AL, byte [litere+ESI] 
                                           cbw
                                           cwde 
                                           push dword EAX
                                           push dword format_caracter ;format is %c
                                           call [printf]
                                           add ESP, 4*2 
                                           inc ESI 
                                           pop ECX   
                                loop afisare_litere

===>> UPDATE <<===== THIS IS THE CODE THAT IS ACTUAL WORKING

  mov [dim_sir_litere], EBX  ;the length of letters array
                            mov EBX,[dim_sir_litere] ;number of letters from litere array
                            mov ESI,litere
                            afisare_litere: 
                                       lodsb
                                       push dword EAX
                                       push dword format_caracter ;format is %c
                                       call [printf]
                                       add ESP, 4*2 
                                       sub EBX,1
                                       cmp EBX,0
                                       jne afisare_litere
  • 1
    Each character is 1 byte, you should store 1 byte not 4. That's not your problem though. You did not show how you are trying to print, also you did not say if the `numere` prints correctly. Use a debugger to verify your array content, then you will know where to look for the problem. – Jester Jan 22 '20 at 17:07
  • @Jester take a look now, still not working. It is not working for numere either – Andrei Cezar Jan 22 '20 at 17:15
  • You don't need cbw / cwde. Just load with `movsx eax, byte [litere + ESI]`. In fact, a `%c` format will ignore high bytes so you can pass whatever garbage you want in high bytes. (But `movzx` as a byte load is still your best bet.) Also, just use a different reg for your loop counter so you don't have to push/pop ECX. – Peter Cordes Jan 22 '20 at 17:19
  • @PeterCordes i tried your method but still not working :( – Andrei Cezar Jan 22 '20 at 17:43
  • 1
    Do you write `ebx` to `dim_sir_litere` somewhere? Please post [mcve]. The code works fine if I complete it to something that can be run. – Jester Jan 22 '20 at 17:51
  • mov [dim_sir_litere],byte EBX ;the length of letters array --> THIS is right after end of loop repeta I use olly dbg, and i saw that i have no value in litere array when i am trying to print, i don't know why – Andrei Cezar Jan 22 '20 at 17:57
  • That should not be a `byte` though. Anyway, it works here. – Jester Jan 22 '20 at 17:58
  • @Jester i removed they "byte", but as i said, i don't have any values in litere array after compare block – Andrei Cezar Jan 22 '20 at 18:00
  • @Jester if you want i can give you all my code, 144 lines – Andrei Cezar Jan 22 '20 at 18:06
  • 1
    Yeah, full code would be nice. – Jester Jan 22 '20 at 22:42
  • https://wetransfer.com/downloads/9332de06c87df5419e6713d7519abd6c20200123083951/2bb1d4fe6888756a93a430674497cbf420200123083951/778e0c – Andrei Cezar Jan 23 '20 at 08:41
  • `push dword [EAX]` is wrong. You want `push eax`, no brackets because that would try to dereference a pointer which your character obviously isn't. The code in the question was correct, but the code on the link does not match it. Make sure you always show the code you are actually using. – Jester Jan 23 '20 at 12:49
  • it is not working, i tried both ways. Now i tried something with lods , but is not working anyway – Andrei Cezar Jan 23 '20 at 12:52
  • Also `edx` is a caller-saved register, `printf` will destroy it so you need to preserve that for yourself. The first character should be printed nevertheless. – Jester Jan 23 '20 at 12:54
  • i changed it to ebx, so i do not have problems with it – Andrei Cezar Jan 23 '20 at 13:00
  • `dim_sir_numere` and `dim_sir_litere` need to be `dd` not `db`. – Jester Jan 23 '20 at 13:20
  • check this one, now it print one letter https://wetransfer.com/downloads/ec92c9975d13662f2760586d787b11d620200123132401/90e14ba5e1ccda60313e49cc17b4adbe20200123132401/69ad2e – Andrei Cezar Jan 23 '20 at 13:24
  • You want `lodsb` not `lodsd`. Also you do not need the `inc esi` as that is part of `lodsb`. – Jester Jan 23 '20 at 13:47
  • IT IS WORKING!! but just for letters for numbers not – Andrei Cezar Jan 23 '20 at 13:52
  • Works for numbers too, here. – Jester Jan 23 '20 at 13:59
  • my numbers are in ascii, do i need to change something before print? – Andrei Cezar Jan 23 '20 at 14:02
  • 1
    You can print them with `%c`, the same as the letters. They are, as you say, ascii. – Jester Jan 23 '20 at 14:03
  • 1
    Yes, you are right. Thank you so much for your time, i really have no idea what i would have done haha. Thank you! – Andrei Cezar Jan 23 '20 at 14:05

0 Answers0