News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Output buffer writing to console and writing to file

Started by nickc86, May 09, 2017, 12:09:23 PM

Previous topic - Next topic

nickc86

Here is my code so far. Everything works, except the output is not correctly formatted.

In the console, I am getting everything on one line. Each Time is x should be on it's own line.
In the file, it is writing the whole input buffer (message + n null chars = 71), File should look like this:

Time is 1
Time is 2
.
.
.
Time is n


I am getting:
Time is 1
                                                                                          Time is 2
                                                                                          Time is 3
.
.
.
                                                                                          Time is N

Here are the important code pieces

INCLUDE Irvine32.inc

.data
;-----DATA FOR FILE HANDLING-----
fileName byte 41 dup (?)
fileHandle HANDLE ?
;-----END OF FILE HANDLING DATA-----

;-----MISC BUFFERS-----
BUFFSIZE equ 71
inputBuffer byte 41 dup (?)   ;input buffer
outBuffer byte BUFFSIZE dup (?)
outBufferPos dword 0
numChars dword ?
;-----END-----

;-----PROGRAM VARIABLES-----
useEcho byte ?    ;if echo is being used. 1 for true, 0 for false
time word 1    ;start time at 1
;-----END PROG VARIABLES-----

;-----MESSAGE PARTS-----
timeis byte "Time is "
;-----END MESSAGE PARTS

;macros
writeLine MACRO text
    LOCAL string   ;local label
    .data
    string BYTE text,0
    .code
    push edx
    mov edx,OFFSET string
    CALL WriteString
    pop edx
ENDM

.code
main PROC
call getFileName
call makeFile
cmp error, 1
je ext
mov ecx, 22
lp2:
call addTime
dec ecx
jne lp2

ext:
exit
main ENDP

;-------------------------------------------------------
; getFileName procedure
; Desc: this will ask the user to enter the file-
; name for storing output
;-------------------------------------------------------
getFileName PROC
    pushad    ;push all registers
    begin:    ;beginning of loop
    writeLine "Please enter the filename (up to 40 characters): "   ;print message
    mov edx, OFFSET fileName    ;get the address of inputbuffer
    mov ecx, SIZEOF fileName    ;get the size of the input buffer
    CALL ReadString    ;read in user input
    cmp eax, 0    ;did they input something?
    je begin    ;if not, reprompt
    call Crlf    
    call WriteString
    popad
    ret
getFileName ENDP
;-------------------------------------------------------
;-------------------------------------------------------
; makeFile procedure
; Desc: this will ask the user to enter the file-
; name for storing output
;-------------------------------------------------------
makeFile PROC
    mov edx, OFFSET fileName
    CALL CreateOutputFile
    cmp eax, INVALID_HANDLE_VALUE
    je err
    mov fileHandle, eax
    jmp ext
    err:
    writeLine "Error opening file. Program quitting"
    mov error, 1
    ext:
ret
makeFile ENDP
;-------------------------------------------------------

;-------------------------------------------------------
; addTime
; Desc: this procedure will write current time
; to the command line and to the output file
;         It will then inc time.
;-------------------------------------------------------
addTime PROC
    pushad ;push the registers
    CALL clearOutputBuffer ;clear the output buffer
    mov ecx, LENGTHOF timeis ;get length of message in ecx
    mov outBufferPos, ecx ;update the buffer pos
    mov esi, OFFSET timeis ;point to the message part
    mov edi, OFFSET outBuffer ;point to the buffer
    cld ;clear direction
    rep movsb ;copy the string
    mov outBuffer[8], 20h ;add a space
    mov eax, 0
    mov ax, time
    mov ecx, 0
    mov bx, 10
    mov edi, OFFSET outBuffer
    add edi, outBufferPos
    lp1:
   mov edx, 0
   div bx
   or dl, 30h
   push edx
   inc ecx
   cmp ax, 0
   jne lp1
   mov edx, ecx
    lp2:
   mov eax, 0
   pop eax
   mov byte ptr[edi], al
   inc edi
   dec ecx
   jne lp2
    mov byte ptr[edi], 0
    inc edi
    inc outBufferPos
    mov byte ptr[edi], 0dh
    inc edi
    inc outBufferPos
    mov byte ptr[edi], 0ah
    inc outBufferPos
    mov edx, OFFSET outBuffer ;prepare to print
    CALL WriteString ;print message
    mov eax, fileHandle
    mov ecx, BUFFSIZE
    CALL WriteToFile
    inc time
    popad
ret
addTime ENDP
;-------------------------------------------------------

;-------------------------------------------------------
; clearOutputBuffer
; Desc: this procedure will clear the output
; buffer by placing all null chars in all slots
; and reseting the buffer position back to 0
;-------------------------------------------------------
clearOutputBuffer proc
    pushad
    mov ecx, 0
    lp1:
   cmp ecx, BUFFSIZE
   je ext
   mov outBuffer[ecx], 0
   inc ecx
   jmp lp1
    ext:
    mov outBufferPos, 0
    popad
ret
clearOutputBuffer endp
;-------------------------------------------------------

END main