Masm读取输入文件到数组

时间:2014-03-09 01:13:23

标签: arrays assembly masm irvine32

我正在使用Masm和Irvine32库。我是Assembly的新手,我在将文件的内容转换成数组时遇到了麻烦。一旦我读入文件并转换它,我应该能够总结数组或其他任何我我需要处理它,但是现在我将我的数组转换为ascii后将其转换为int,以便将其打印出来。我相信我需要做的是在输入文件中读取,该文件包含由空格分隔的数字列表,将ascii转换为int,存储在数组中,最后转换回ascii并输出结果。这是对的吗?

我的输入看起来像这样,用空格分隔数字:

24 31 4 63 9 11 17 3 56 37

到目前为止,这是我的计划:

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

现在我的程序正确读取整个文件,如果我打印缓冲区数组,所有内容都正确输出。我可以将我的数组转换为int但我无法正确地将其转换回ascii。我没有正确循环或递增吗?使用上面的输入,我的输出(转换回ascii后)是8589793965,这是不正确的。我无法弄清楚我做错了什么。我试着读一个数字,除以十,加上余数30小时,这是正确的吗?我似乎无法正确找到数字的第二位数。

非常感谢任何帮助。感谢

0 个答案:

没有答案