News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Weird Output

Started by DaTrinity22, December 11, 2017, 09:59:10 AM

Previous topic - Next topic

DaTrinity22

Hey guys, it's me again. So I am continuing to work on this project and I just wrote the outputOlympian procedure. For some reason, when I output the medal count and the total, the output looks like this:

Enter a filename: C:\Users\Griffin\Desktop\Project5\input.txt
Name: Michael Phelps
Country: USA
Gender: M
Medals: 
Total Medals: ¿°ⁿ

I will attach a zip of the VS folder again. I really don't understand why it's not working this time as the values I'm trying to print out are simply integers.

include Irvine32.inc

olympian STRUCT
athlete BYTE 32 DUP('a') ; 32 bytes
country BYTE 32 DUP('c') ; 32
gender BYTE 'x' ; 1
ALIGN DWORD ; add 3 bytes
medals DWORD -1,-1,-1 ; gold silver bronze (96)
olympian ENDS ; 164 total

; define some constants for global use
FSIZE = 100 ; max file name size
BSIZE = 1000 ; buffer size
LINESIZE = 100 ; max input line size
CR = 0Dh ; c/r
LF = 0Ah ; line feed
ASTERISK = 2Ah ; asterisk for new entry
NULL = 00h ; null character
SPACE = 20h ; space character
MAXNUM = 5 ; number of olympians

.data
filename BYTE FSIZE DUP(?) ; array to hold the file name
buffer BYTE BSIZE DUP(?) ; buffer to hold the file contents
prompt BYTE "Enter a filename: ",0 ; prompt for a string
ferror BYTE "Invalid input...",0 ; error message


olist olympian <>,<>,<>,<>,<> ; list of 5 olympians

; for output listing
outname    BYTE "Name: ",0
outcountry BYTE "Country: ",0
outgender  BYTE "Gender: ",0
outmedals  BYTE "Medals: ",0
outtotal   BYTE "Total Medals: ",0

.code
main PROC
    ; load the data from a file - push the arguments in reverse order
push BSIZE ; data buffer size
push OFFSET buffer ; point to the start of the buffer
push FSIZE ; file name size
push OFFSET filename ; point to the start of the string
push OFFSET prompt ; output the prompt
call loadFile ; load the buffer
cmp eax,0 ; nothing read, bail out
jz ERROR

;;;;;DELETE THE FOLLOWING CODES OF LINE BELOW, USED SIMPLY FOR TESTING
push OFFSET olist ;
push OFFSET buffer ;
push LINESIZE ;
call loadALLOlympians ;

push OFFSET olist ;
call outputOlympian ;

; file loaded correctly, process and output the data

jmp DONE ; done processing data, jump to the end
ERROR:
    mov edx,OFFSET ferror ; output error message
call WriteString ; uses edx
call CrLf
DONE:
call WaitMsg ; wait for user to hit enter
invoke ExitProcess,0 ; bye
main ENDP



; prompts for a file name and reads contents into a buffer
; receives:
; [ebp+8] = (null terminated) prompt string
; [ebp+12] = pointer to file name string
; [ebp+16] = max size of file name string
; [ebp+20] = pointer to buffer array
; [ebp+24] = max size buffer
; returns:
; eax = number of bytes read, zero on an error
loadFile PROC
push ebp ; save the base pointer
mov ebp,esp ; base of the stack frame
sub esp,4 ; create a local variable for the return value
pushad ; save all of the registers (lazy)
; prompt for the file name
    mov edx,[ebp+8] ; output the prompt
call WriteString ; uses edx 
; get the file name, open the file
mov edx,[ebp+12] ; point to the start of the file name string
mov ecx,[ebp+16] ; max size for file name
call ReadString ; load the file name (string pointer in edx, max size in ecx)
call OpenInputFile ; open the file (expects name in edx, max size in ecx)
mov ebx,eax ; save the file pointer (returned in eax)
cmp eax,INVALID_HANDLE_VALUE ; check for a valid file pointer
je BAD ; bail out on a failure
; load the buffer wit the contents of the file
mov edx,[ebp+20] ; point to the start of the buffer
mov ecx,[ebp+24] ; max size of the buffer
call ReadFromFile ; gets file handle from eax (loaded above)
mov DWORD PTR [ebp-4],eax ; save the number of bytes in local variable
mov eax,ebx ; restore the file pointer for closing (saved above)
call CloseFile ; close the file
jc BAD ; if carry fag set, it's an error
jmp OK
BAD:
call WriteWindowsMsg ; got an error, display it
mov DWORD PTR [ebp-4],0 ; error: set the number of bytes read to zero
OK: ; clean up
popad ; restore the registers
mov eax,DWORD PTR [ebp-4] ; save the number of bytes read for return in eax
mov esp,ebp ; remove local varfrom stack
pop ebp
ret 20
loadFile ENDP



; copies bytes from the input buffer to a member of the Olympian struct
; RECEIVES:
; [ebp+8] = LINESIZE- the max number of bytes to read
; [ebp+12] = OFFSET Olympian.<member>- pointer to a member of the Olympian struct
; [ebp+16] = OFFSET buffer- pointer to the next character to read from the buffer
; RETURNS:
; eax = pointer to the next character to read from the buffer
bufferCopy PROC USES ebx ecx edx
LOCAL ctr:BYTE ; creates a local counter variable
mov ctr, 0 ; sets counter to 0
mov ebx, [ebp+16] ; stores address of char in buffer pointed to by the pointer param in ebx (B for Buffer)
mov ecx, [ebp+12] ; stores address of the member of the Olympian struct param in edx
jmp START ; jump to the start label since counter is zero and no bytes have been read.
NEXTCHAR:
inc ebx ; moves to the next char in the buffer
inc ecx ; moves to the next empty character in member of Olympian struct
START:
mov dl, [ebx] ; moves the next character in the buffer to register al
cmp dl, CR ; checks if next character is a carriage-return
JZ ENDLINE ; if the next char is a carr-ret, jump to ENDLINE label
mov [ecx], dl ; move current char in buffer to next empty char in member of Olympian struct (register al is now clear)
inc ctr ; increments the counter
mov dl, ctr ; moves the counter to register al
cmp dl, [ebp+8] ; checks if the counter equals or exceeds LINESIZE
JC NEXTCHAR ; if counter is less than LINESIZE, jump to READCHAR label (loop)
jmp EXITPROC ; else if counter is not less than LINESIZE, jump to EXIT label (exit proc)
ENDLINE:
mov dl, NULL ; moves the null character into register al
mov [ecx], dl ; null-terminates the member of the Olympian struct
EXITPROC:
add ebx, 2 ; skips the line feed character in the buffer
mov eax, ebx ; moves the next char to be read from buffer into ebx to be returned (ecx+ctr+2 skips the line feed char)
ret 12
bufferCopy ENDP


; reads information from the buffer and loads it into an Olympian struct
; RECEIVES:
; [ebp+8] = LINESIZE- the max number of bytes to read for each member of the struct (passed to bufferCopy)
; [ebp+12] = OFFSET buffer- pointer to the beginning of the information for the current Olympian
; [ebp+16] = OFFSET olist- pointer to the next element in the array of Olympian structs
; RETURNS:
; eax = pointer to the beginning of the information for the next Olympian
loadOlympian PROC USES ebx ecx edx esi
LOCAL medal[3]:BYTE, numParse:BYTE ; creates local variables
push [ebp+12] ; passes the buffer to bufferCopy
mov ebx, [ebp+16] ; moves the address of the athlete member into ebx
push ebx ; passes the athlete member of the current Olympian to bufferCopy
push [ebp+8] ; passes the max line size to bufferCopy
call bufferCopy ; calls bufferCopy

push eax ; passes the address of the next char to read from the buffer to bufferCopy
add ebx, SIZEOF Olympian.athlete ; moves the address of the country member into ebx
push ebx ; passes the country member of the current Olympian to bufferCopy
push [ebp+8] ; passes the max line size to bufferCopy
call bufferCopy ; calls bufferCopy

push eax ; passes the address of the next char to read from the buffer to bufferCopy
add ebx, SIZEOF Olympian.country ; moves the address of the gender member into ebx
push ebx ; passes the gender member of the current Olympian to bufferCopy
push [ebp+8] ; passes the max line size to bufferCopy
call bufferCopy ; calls bufferCopy

add ebx, 3 ; skips 3 bytes to align the Double Words
mov numParse, 0 ; sets numParse to 0
add ebx, SIZEOF Olympian.gender ; moves the address of the medals array into register ebx

MEDALS:
push eax ; passes the address of the next char to read from the buffer to bufferCopy
lea esi, medal ; gets the address of the medal string
push esi ; passes the address of the string used to store the medal counts to bufferCopy
push [ebp+8] ; passes the max line size to bufferCopy
call bufferCopy ; calls bufferCopy

push eax ; pushes eax to the stack to preserve the address of next char to read from buffer
mov edx, esi ; moves the address of the string containing # of gold medals into edx for passing to ParseInteger32
mov ecx, SIZEOF medal ; moves the length of the string into ecx for passing to ParseInteger32
call ParseInteger32 ; calls ParseInteger32
mov [ebx], eax ; moves the gold medal count into the medals array
add ebx, TYPE Olympian.medals ; increases the index of the array by 1
inc numParse ; increments numParse
pop eax ; pop eax from the stack to get the address of the next char to read from buffer

cmp numParse, 3 ; checks to see if numParse equals 3
jnz MEDALS ; if numParse is less than 3, jump to MEDALS label.

ret 12
loadOlympian ENDP

; calls loadOlympian to transfer data from the memory buffer into the array of olympian structs
; RECEIVES:
; [ebp+8] = LINESIZE - the max number of bytes to read for each member of the struct (passed to loadOlympian)
; [ebp+12] = OFFSET buffer- pointer to the beginning of the information to enter into the olympian structs
; [ebp+16] = OFFSET olist- pointer to the beginning of the array of Olympians
; RETURNS:
; eax = number of Olympians read
loadAllOlympians PROC USES ebx ecx
LOCAL numOlympians:BYTE ; creates local variable

mov numOlympians, 0 ; sets numOlympians to 0
mov ebx, [ebp+16] ; moves the address of olist into ebx
mov eax, [ebp+12] ; moves the address of the buffer into eax

LOAD:
push ebx ; passes the address of the next Olympian to load to loadOlympian
push eax ; passes the address of the information in the buffer to load into the next Olympian to loadOlympian
push [ebp+8] ; passes the max line size to loadOlympian
call loadOlympian ; calls loadOlympian

inc numOlympians ; increments numOlympians
cmp numOlympians, MAXNUM ; checks to see if the olist is full
jz EXITPROC ; if olist is full, jump to label EXIT
mov ecx, [eax] ;
cmp ecx, "*" ; checks to see if the next char is an asterisk
jz EXITPROC ; if the next char is not an asterisk
add ebx, TYPE Olympian ; increases the index of olist by 1
jmp LOAD ; jump to LOAD

EXITPROC:
mov al, numOlympians ; moves numOlympians into eax to be returned
ret 12 ;

loadAllOlympians ENDP

; outputs the contents of one olympian struct to the console in a well formatted manner
; RECEIVES:
; [ebp+8] = OFFSET olist[<index>]- address of the next struct object
; RETURNS
; n/a
outputOlympian PROC USES eax ebx edx
LOCAL total:BYTE ; creates local variable
mov ebx, [ebp+8] ; moves the address of the struct into ebx
mov total, 0 ; sets total to 0

mov edx, OFFSET outname ; passes the name label to WriteString
call WriteString ; calls WriteString
mov edx, ebx ; passes the athlete member of the struct to WriteString
call WriteString ; calls WriteString
call CrLf ; ends the line
add ebx, SIZEOF Olympian.athlete ; moves the address of the country member into ebx

mov edx, OFFSET outcountry ; passes the country label to WriteString
call WriteString ; calls WriteString
mov edx, ebx ; passes the country member of the struct to WriteString
call WriteString ; calls WriteString
call CrLf ; ends the line
add ebx, SIZEOF Olympian.country ; moves the address of the gender member into ebx

mov edx, OFFSET outgender ; passes the gender label to WriteString
call WriteString ; calls WriteString
mov edx, ebx ; passes the gender member of the struct to WriteString
call WriteString ; calls WriteString
call CrLf ; ends the line
add ebx, SIZEOF Olympian.gender ; moves the address of the gold medals member into ebx
add ebx, 3 ; aligns the doublewords

mov edx, OFFSET outmedals ; passes the medals label to WriteString
call WriteString ; calls WriteString
mov dl, [ebx] ; gets the value of gold medals for the total
add total, dl ; adds gold medals to the total
mov edx, ebx ; passes the gold medals to WriteString
call WriteString ; calls WriteString
;mov edx, OFFSET space ; passes a space char to WriteString
;call WriteString ; calls WriteString
add ebx, TYPE Olympian.medals ; moves the address of the silver medals member into ebx

mov dl, [ebx] ; gets the value of silver medals for the total
add total, dl ; adds silver medals to the total
mov edx, ebx ; passes the silver medals to WriteString
call WriteString ; calls WriteDec
;mov edx, OFFSET space ; passes a space char to WriteString
;call WriteString ; calls WriteString
add ebx, TYPE Olympian.medals ; moves the address of the bronze medals member into ebx

mov dl, [ebx] ; gets the value of bronze medals for the total
add total, dl ; adds bronze medals to the total
mov edx, ebx ; passes the bronze medals to WriteString
call WriteString ; calls WriteString
call CrLf ; ends the line

mov edx, OFFSET outtotal ; passes the total label to WriteString
call WriteString ; calls WriteString
lea esi, total ; moves the address of total into esi
mov edx, esi ; passes the total medals to WriteString
call WriteString ; calls WriteString
call CrLf ; ends the line
call CrLf ; ends the line again to keep each olympian's info separate
outputOlympian ENDP
END main

DaTrinity22

NEVERMIND, I MADE A STUPID MISTAKE THAT I MISSED BECAUSE I HAVE HAD NO SLEEP.