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?
off the bat your enterMessage is not null terminated... add a comma and 0 to the end of it
Thanks I changed it but the message still pops up :dazzled:
.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?
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
So why is it when I run it and put in 200 4 times, the message doesn't pop up it works fine
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
so do i have to change the registers?
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
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