2

I am using Masm with Irvine32 library. I am new to Assembly and I'm having trouble with converting the contents of a file into an array once I read it in. Once I read in the file and convert it, I should be able to sum up the array or whatever else I need to do with it but right now I'm having trouble with converting my array back to ascii after converting it to int, in order to print it out. I believe what I need to do is read in an input file that contains a list of numbers separated by spaces, convert ascii to int, store in an array, and finally, convert back to ascii and output the results. Is this correct?

My input looks something like this, with spaces to separate the numbers:

24 31 4 63 9 11 17 3 56 37

Here is my program so far:

INCLUDE Irvine32.inc
.data
TEN     dword           10  
BUFFER_SIZE =       5000
buffer      dword       BUFFER_SIZE dup (?)
bytesRead   dword           0
inFilename  byte        "input.txt", 0
infileH     dword           0
cnt     dword           0
ary     dword           20 dup (?) ; Array for storing converted ascii to int
    bry     dword           20 dup (?)
    size            dword           10
.code
main PROC
call        zeroOut

; Open input file
mov     edx,    OFFSET inFilename
call        OpenInputFile
mov     infileH,    eax

; Read file into buffer
mov     edx,    OFFSET buffer
mov     ecx,    BUFFER_SIZE
call        ReadFromFile
mov     bytesRead, eax

; Close input file
mov     eax,    infileH
call        CloseFile

    ; Convert ascii to int and store in ary
call        zeroOut
lea     esi,    OFFSET buffer
lea     edi,    OFFSET ary
mov edx, size
L1:
    call convertasciitoint
    mov [edi],  eax
    inc edi
    inc esi
    dec edx
    call DumpRegs
    cmp edx, 0
    jne L1
call    zeroOut

    ; Convert int to ascii for printing
lea     esi,    OFFSET ary
    lea     edi,    OFFSET bry
mov ebx,    size
L2:
    call    convertinttoascii
    mov [edi],  eax
    inc esi
    inc edi
    dec ebx
    cmp ebx,    0
    jne L2

    ; Print output
lea     esi, OFFSET bry
call        myLine
exit
main ENDP

convertasciitoint PROC
mov ecx,    0
mov eax,    0
nextDigit:
    mov bl, [esi]
    cmp bl, '0'
    jl  outOfHere
    cmp bl, '9'
    jg  outOfHere
    add bl, -30h
    imul    eax,    10
    add eax,    ebx
    ;mov    [esi], eax
    ;mov    [edi], eax
    inc ecx
    inc esi
    ;inc    edi
    jmp nextDigit
outOfHere:
    mov cnt,    ecx
    ret
convertasciitoint ENDP

convertinttoascii PROC
mov ecx,    cnt
nextDigit:
    mov al, [esi]
    div TEN
    mov eax,    0
    mov al, dl
    add al, 30h
    ;mov    [edi], dl
    ;mov    dl, [esi]
    ;inc    esi
    ;inc    edi
    call    DumpRegs
    dec ecx
    cmp ecx,    0
    jne nextDigit
    ret
convertinttoascii ENDP

myLine PROC
nextChar:
    mov al, [esi]
    inc esi
    call    WriteChar
    cmp al, NULL
    jne nextChar
    ret
myLine ENDP

zeroOut PROC
mov eax,    0
mov ebx,    0
mov ecx,    0
mov edx,    0
ret
zeroOut ENDP
END main

Right now my program reads in the entire file correctly, if I print the buffer array everything is output correctly. I can convert my array to int but I can't convert it back to ascii correctly. Am I not looping or incrementing correctly? Using the input above, my output (after converting back to ascii) is 8589793965, which isn't correct. I can't figure out what I'm doing wrong. I'm trying to read in a number, divide by ten and add 30h to the remainder, is this correct? I can't seem to get to the second digit of the number correctly.

Any help would be much appreciated. Thanks

rkhb
  • 14,159
  • 7
  • 32
  • 60
user3397328
  • 21
  • 1
  • 3
  • Did you use a debugger to step through the code and see where it goes wrong? – Jester Mar 09 '14 at 01:16
  • We haven't really gone over how to debug yet. I used DumpRegs in my loops to see what the output was each time it looped through but I couldn't figure out what exactly was wrong. It looks like it stops each time it gets to a space when I'm trying to convert my array to int because a space is not between 0 or 9. Is this my problem? I could use cmp eax, ' ' to see if it is a space but not sure where to go after that. – user3397328 Mar 09 '14 at 01:23
  • You could move the `inc esi` up to before the check is done, so that it consumes the space. You should really get yourself a debugger, that should be one of the first things to do so you can see what the cpu is doing and find your own mistakes. `DumpRegs` is really no substitute and it's really tedious too. – Jester Mar 09 '14 at 01:26
  • In which function? I tried moving it in both convertasciitoint and convertinttoascii and it didn't work. For asciitoint it didn't change anything and for inttoascii it changed my output from 32 and 30 to 32 for the first digit and 62 for the second digit. I will have to check on figuring out how to use the debugger. – user3397328 Mar 09 '14 at 01:54
  • `asciitoint` is eating the text, so that should consume the space before the next number is processed. There may be other issues with the code. – Jester Mar 09 '14 at 01:56
  • It works fine with the first number but has trouble with the next number after the space. Should I convert the entire array from ascii to int in my asciitoint function or should I put a loop in main that calls asciitoint and increments esi to the next number each time so that I'm only converting one number at a time? I've tried it both ways but still not having any luck. – user3397328 Mar 09 '14 at 02:13
  • I appreciate your help. Alright, I got my asciitoint working correctly but when I try to convert back to ascii for printing using inttoascii, I'm not getting the correct numbers. I've updated my code above to reflect the changes. – user3397328 Mar 09 '14 at 17:38

0 Answers0