The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: jwill22 on February 10, 2011, 08:01:01 PM

Title: GPA Program help
Post by: jwill22 on February 10, 2011, 08:01:01 PM
Ok so I'm working on this program that should read 4  numbers and calculate the gpa. For the sake of time I'm just going under the assumption that all 4 classes are 4 credit hours. So I'm having a problem with this one little thing. After I enter 4 numbers a box comes up saying the program has encountered a problem and needs to close. I think it has something to do with my .if statements.
Here's my code:
INCLUDE Irvine32.inc


.data
array1 SWORD 4  DUP(?)
enterMessage BYTE " Please enter 4 numbers"
.code
main PROC
mov ecx,4
mov esi, OFFSET array1 - 2
mov edx,OFFSET enterMessage
call WriteString
call Crlf

loop1:
add esi,2
call ReadInt
;call WriteInt
mov [esi],eax
call Crlf
loop loop1

mov ecx,4
mov esi,OFFSET array1 - 2
;mov ebx,0
step:

add esi,2
mov ax,[esi]
;.if (sdword ptr eax < 0)
;mov [edi],eax

.if (sword ptr ax >=90 && sword ptr ax <=100)
mov bx,16
mov [edi],bx
call WriteInt

.ELSEIF (sword ptr ax>=80 && sword ptr ax<=89)
mov bx,12

mov [edi],bx


.ELSEIF (sword ptr ax >=70 && sword ptr ax<=79) ;grade from 70-79 display cMessage
mov bx,8
mov [edi],bx


.ELSEIF (sword ptr ax >=60 && sword ptr ax <=69) ;grade from 60-69 display dMessage
mov bx,4
mov [edi],bx

.ELSEIF (sword ptr ax >=0 && sword ptr ax <=59) ;grade from 0-59 display fMessage
mov bx,0
mov [edi],bx

.endif ;end if

loop step














   
exit

main ENDP
END main



Once I get pass that point I think I can handle the rest. Can somebody help me out and tell me what's wrong?
Title: Re: GPA Program help
Post by: Gunner on February 10, 2011, 09:56:59 PM
off the bat your enterMessage is not null terminated... add a comma and 0 to the end of it
Title: Re: GPA Program help
Post by: jwill22 on February 10, 2011, 10:21:01 PM
Thanks I changed it but the message still pops up  :dazzled:
Title: Re: GPA Program help
Post by: brethren on February 10, 2011, 10:30:13 PM

.if (sword ptr ax >=90 && sword ptr ax <=100)
mov bx,16
mov [edi],bx
call WriteInt


edi hasn't been initialised to any intelligent value so why are you using it as a pointer?
Title: Re: GPA Program help
Post by: brethren on February 10, 2011, 10:34:12 PM
loop1:
add esi,2
call ReadInt
;call WriteInt
mov [esi],eax
call Crlf
loop loop1


even though this code seems to work :P you are writing a 4 byte value(eax) straight into a 2 byte array element on each iteration of the loop. mov [esi], ax should work
Title: Re: GPA Program help
Post by: jwill22 on February 10, 2011, 10:47:48 PM
So why is it when I run it  and  put in 200 4 times, the message doesn't pop up it works fine
Title: Re: GPA Program help
Post by: brethren on February 11, 2011, 12:35:17 AM
watch it step by step in a debugger00404000  C8 00 C8 00 C8 00 C8 00 00 00 6C 65 61 73 65 20  È.È.È.È...lease
00404010  65 6E 74 65 72 20 34 20 6E 75 6D 62 65 72 73 00  enter 4 numbers.




thats what happens when you enter 200 4 times, it overwrites the end of the array by 2 bytes deleteing the first two bytes of enterMessage. its a subtle bug :P
Title: Re: GPA Program help
Post by: jwill22 on February 11, 2011, 12:58:51 AM
so do i have to change the registers?
Title: Re: GPA Program help
Post by: brethren on February 11, 2011, 01:27:39 AM
i'm not 100% sure on what it is you need to do but this doesn't crash

INCLUDE Irvine32.inc


.data
array1 SWORD 4  DUP(?)
enterMessage BYTE " Please enter 4 numbers", 0

AGrade BYTE "You got an A", 13, 10, 0
BGrade BYTE "You got a B", 13, 10, 0
CGrade BYTE "You got a C", 13, 10, 0
DGrade BYTE "You got a D", 13, 10, 0
FGrade BYTE "You got an F!", 13, 10, 0
Invalid BYTE "Not a valid grade", 13, 10, 0

.code
main PROC
mov ecx,4
mov esi, OFFSET array1 - 2
mov edx,OFFSET enterMessage
call WriteString
call Crlf

loop1:
add esi,2
call ReadInt
;call WriteInt
mov [esi],ax
call Crlf
loop loop1

mov ecx,4
mov esi,OFFSET array1 - 2
;mov ebx,0
step:

add esi,2
mov ax,[esi]

.if (sword ptr ax >=90 && sword ptr ax <=100)
mov edx, OFFSET AGrade

.ELSEIF (sword ptr ax>=80 && sword ptr ax<=89)
mov edx, OFFSET BGrade

.ELSEIF (sword ptr ax >=70 && sword ptr ax<=79) ;grade from 70-79 display cMessage
mov edx, OFFSET CGrade

.ELSEIF (sword ptr ax >=60 && sword ptr ax <=69) ;grade from 60-69 display dMessage
mov edx, OFFSET DGrade

.ELSEIF (sword ptr ax >=0 && sword ptr ax <=59) ;grade from 0-59 display fMessage
mov edx, OFFSET FGrade
.ELSE
mov edx, OFFSET Invalid

.endif ;end if

call WriteString

loop step















exit

main ENDP
END main
Title: Re: GPA Program help
Post by: dedndave on February 11, 2011, 01:29:56 AM
use BYTE, WORD, DWORD, DB, DW, DD
get rid of SWORD - it's causing you some confusion
"sword ptr ax" - you don't need to specify a size, here - AX can't be anything but a word
your array is WORD sized, and you are writing DWORDS into it