Question: This is MASM x 8 6 Assembly. Even with the code that was provided in my previous follow - up question this is the output
This is MASM x Assembly. Even with the code that was provided in my previous followup question this is the output I am receiving. This is in Visual Studio my code: INCLUDE Irvineinc
stack
; macros
mGet MACRO promptAddr, bufferAddr, bufferSize
lea edx, promptAddr
call WriteString ; Display the prompt
mov edx, bufferAddr
mov ecx, bufferSize
call ReadString ; Get user input
ENDM
mDisplayString MACRO stringAddr
mov edx, stringAddr
call WriteString ; Display a string
ENDM
mDisplayChar MACRO charValue
mov al charValue
call WriteChar ; Display a character
ENDM
; constants
TEMPSPERDAY EQU ; Expected number of temperatures
DELIMITER EQU ; Delimiter for temperatures
BUFFER EQU ; Buffer size
data
getFileName BYTE "Enter file name:
giveError BYTE "Error occurred trying to open this file.
outputMsg BYTE "The temperatures in the correct order:
tempFileName BYTE C:tempTempstxt
fileBuffer BYTE BUFFER DUP ; Buffer to hold file contents
arrayTemps SDWORD TEMPSPERDAY DUP ; Array for temperatures
code
main PROC
; Prompt for file name
mGet getFileName, OFFSET tempFileName, BUFFER
; Open the file
mov edx, OFFSET tempFileName
call OpenInputFile
JC FileError ; Jump to error handler if file not found
; Read file contents
mov edx, OFFSET fileBuffer
mov ecx, BUFFER
call ReadFromFile
cmp eax,
je FileError ; Handle empty file
cmp eax, BUFFER
jae FileError ; Handle buffer overflow
; Parse and store temperatures
PUSH OFFSET fileBuffer
PUSH OFFSET arrayTemps
call ParseTempsFromString
; Display the parsed temperatures in reverse order
mDisplayString OFFSET outputMsg
PUSH OFFSET arrayTemps
call WriteReverse
; Exit program
jmp EndProg
FileError:
; Handle file errors
mov edx, OFFSET giveError
call WriteString
jmp EndProg
EndProg:
invoke ExitProcess,
main ENDP
; Parse temperatures from the string
ParseTempsFromString PROC
push ebp
mov ebp, esp
mov esi, ebp ; Source buffer address
mov edi, ebp ; Destination array address
xor ebx, ebx ; Accumulate integer values
xor edx, edx ; Sign flag for positive, for negative
ParseLoop:
lodsb ; Load next byte from source buffer
test al al ; Check for null terminator
jz StoreLastValue
cmp al DELIMITER ; Check for delimiter
je StoreValue
cmp al ; Check for negative sign
je NegativeNum
cmp al ; Check for valid numeric range
jb SkipChar
cmp al
ja SkipChar
sub al ; Convert ASCII to numeric
imul ebx, ; Accumulate into integer
add ebx, eax
jmp ParseLoop
NegativeNum:
inc edx ; Set sign flag for negative number
jmp ParseLoop
StoreValue:
test edx, edx
jz PositiveNum
neg ebx ; Convert to negative if sign flag is set
PositiveNum:
mov edi ebx ; Store integer into destination array
add edi, ; Move to next position in array
xor ebx, ebx ; Reset accumulator
xor edx, edx ; Reset sign flag
jmp ParseLoop
SkipChar:
jmp ParseLoop
StoreLastValue:
cmp ebx,
je ExitParseLoop
test edx, edx
jz PositiveLast
neg ebx
PositiveLast:
mov edi ebx
add edi,
ExitParseLoop:
pop ebp
ret
ParseTempsFromString ENDP
; Write the array in reverse order
WriteReverse PROC
push ebp
mov ebp, esp
mov esi, ebp ; Array address
mov ecx, TEMPSPERDAY ; Number of elements
lea esi, esi ecx ; Move to end of array
WriteLoop:
sub esi, ; Move to previous element
mov eax, esi ; Load element into EAX
call WriteInt ; Display the integer
call Crlf ; Print newline
loop WriteLoop
pop ebp
ret
WriteReverse ENDP
END main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
