MASM assembly using Irvine stack calculator error while debugging

0

Exception thrown at 0x00000000 in G1P1.exe: 0xC0000005: Access violation executing location 0x00000000.

This is the error I'm getting at cmp ecx, -1 in the input section of my code. I'm unsure what to do you fix this problem. The program is a form of calculator that pushes numbers to a stack and then once an operator is input it pops the values, performs the operation, and then pushes the result back to the stack.

INCLUDE Irvine32.inc

SPACE_KEY = 20
PWORD  TYPEDEF PTR DWORD
.data
Thestack dword 8 dup(0)
position byte 1 dup(0)
buffersize equ 7
buffer byte buffersize dup(0)
Null equ 0
space equ 32
tab equ 9
plus equ 43 
minus equ 45
multi equ 42
division equ 47

string byte "This is a String:",Null
I byte "input", 13,10,0
II byte "Getnext", 13,10,0
III byte "returned", 13,10,0
IV byte "have Char", 13,10,0

.code
main PROC

input:
MOV edx, OFFSET I
call WriteString
mov edx,offset buffer
mov ecx,buffersize
call readstring
mov ecx, buffersize
call Getnext
cmp ecx, -1
JZ no_char
JMP have_char

no_char: JMP input
have_char:
process_char:
mov  eax,0
mov al, buffer[ecx]
sub eax, 30h
cmp eax, 0
JGE maybenum
JMP notnum
maybenum:
cmp eax, 9
JLE isnum
JMP notnum

notnum:
cmp eax, plus
je isplus
cmp eax, minus
je isminus
cmp eax,multi
je ismulti
cmp eax,division
je isdiv
cmp eax,'X'
je exchange
cmp eax,'N'
je Negate
cmp eax,'V'
je View
cmp eax,'C'
je clear
cmp eax,'Q'
je Quit
jmp badinput


isnum:
mov ecx,0
mov cl, position
cmp ecx, 8
jg full
call pushontostack
jmp input

isplus:
call popoffstack
mov ebx, eax
call popoffstack
add eax, ebx
call pushontostack
jmp input

isminus:
call popoffstack
mov ebx,eax
call popoffstack
sub eax,ebx
call pushontostack
JMP input

isdiv:
call popoffstack
mov ebx,eax
call popoffstack
idiv ebx
call pushontostack
JMP input


ismulti:
call popoffstack
mov ebx,eax
call popoffstack
imul eax,ebx
call pushontostack
JMP input

Exchange:
call popoffstack
push eax
mov ebx,eax
call popoffstack
pop  eax
call pushontostack
mov eax,ebx
call pushontostack

Negate:
call popoffstack
neg eax
call pushontostack
jmp input


View:
call popoffstack
call writeint
call pushontostack
jmp input


Up:

Down:

Clear:
mov position, -1
jmp input

Quit:
jmp endline

badinput:
jmp input
full:
jmp input
endline:


;-----------------------------------------------
Getnext PROC
;
; 
;-----------------------------------------------    

push esi
push eax
mov esi,0
loop1: 
mov al,buffer[esi]
cmp al,space
JE skip
cmp al,tab
JE skip
JNE found
jmp found
skip:
inc esi
cmp ecx,esi
JG loop1
mov ecx,-1
JMP out1
found:
mov ecx,esi 
out1: 
pop eax
;pop es
ret
Getnext ENDP
;-----------------------------------------------
;DisplayErrorMsg PROC
;
; Displays an error message indicating that
; the input stream contains illegal input.
; Receives: nothing. 
; Returns: nothing
;-----------------------------------------------
     ;push  edx
     ;mov     edx,OFFSET errormsg
     ;call  WriteString
     ;pop     edx
     ;ret
;DisplayErrorMsg ENDP

pushontostack proc
push ebx
push ecx
mov ebx, Thestack[ecx]
mov Thestack [ebx],eax
inc position
pop ecx
pop ebx
Ret
pushontostack endp


popoffstack proc
push ebx
push ecx
mov ebx, Thestack[ecx]
mov eax, dword ptr [ebx]
dec position
pop ecx
pop ebx
ret
popoffstack endp

main endp
END main
assembly
masm
irvine32
asked on Stack Overflow Feb 22, 2020 by Shawn Carl • edited Feb 22, 2020 by rkhb

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0