Author Topic: Program issue in MASM  (Read 925 times)

jt75

  • Regular Member
  • *
  • Posts: 12
Program issue in MASM
« on: July 06, 2020, 12:44:45 PM »
Forgive me if I'm posting in the wrong forum, but I'm having an issue with my MASM program, the code itself compiles, but when I go to debug, I get Exception thrown at 0x00403716 in Project.exe: 0xC0000005: Access violation writing location 0x00408000. I'll post where I'm having the issue rather than my whole program to be safe.
Code: [Select]
getName proc
mov esi, namesAddr ; esi points to next empty slot of names array
mov currentNameAddr, esi         ; save address of current name

mov eax, 0
mov al, [edi] ; move first name char into al
and al, 223         ; convert to upper case
mov[esi], al ; move first name char into names array

getNameLoop:
inc esi
inc edi

mov al, ' '
cmp[edi], al
je leaveGetNameLoop

mov eax, 0
mov al, [edi]
mov[esi], al ; move name char into names array, the exception error is here

jmp getNameLoop

leaveGetNameLoop :

inc esi
inc edi ; edi points to next good char in lineIn

mov namesAddr, esi

ret
getName endp

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7532
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Program issue in MASM
« Reply #1 on: July 06, 2020, 03:52:24 PM »
You may need to show us a bit more of the app as this does not tell us much. We need to see your startup code and what architecture you are using.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

TouEnMasm

  • Member
  • *****
  • Posts: 1337
    • EditMasm
Re: Program issue in MASM
« Reply #2 on: July 06, 2020, 03:55:58 PM »
In first,esi edi ebx must be safe when you quit the Proc,add uses esi edi ebx just after PROC.
edi is undeterminate at the start of the proc,the adress he pointed is unknown.

To pass an adress at a PROC,the better practice is to pass it by the stak,as this you see what you do:

Quote
.data
buffer db 50 dup(0)
.code
;-----------------------------
Myfunction PROC uses esi edi ebx pbuffer:DWORD
         mov esi,pbuffer           ;esi point on the buffer
           ;......................
ret
MyFunction endp
;------------------------------- later -------------------
invoke MyFunction,addr buffer




Fa is a musical note to play with CL

jt75

  • Regular Member
  • *
  • Posts: 12
Re: Program issue in MASM
« Reply #3 on: July 06, 2020, 04:20:01 PM »
I'll go ahead and show the whole program. It's basically a link list that will read in grades from a text file. What it's supposed to do is take scores out of 300 and convert them to a grade from 0 to 100, so for example, if someone made a 285, what would that be out of 300, etc.
Code: [Select]
INCLUDE Irvine32.inc

.data

inFileName byte "grades.txt", 0
outFileName byte "results.txt", 0
IFD dword ?
OFD dword ?
head dword LL
LL dword 300 dup(? )
maxGrade dword ?
name_ equ 0
grade equ 4
linky equ 8
arg equ 4
lineIn byte 500 dup(? )
names byte 500 dup(? )
myAry byte 16 dup(? )
endl byte 0dh, 0ah, 0
tab_ byte 09h, 0
lineInAddr dword ?
currentNameAddr dword ?
namesAddr dword names
temp dword ll
tempAddr dword temp
prev dword ?
targetGrade dword ?

.code
main proc
lea edx, outFileName
call CreateOutputFile
mov OFD, eax ; open output file

lea edx, inFileName
call OpenInputFile
mov IFD, eax ; open input file

lea edx, lineIn
mov ecx, 500 ; bytes to read in
mov eax, IFD
call ReadFromFile ; read infile

lea edi, lineIn ; edi points to first byte of input

call getGrade
lea esi, myAry
call atoi
mov maxGrade, eax ; get max grade

mov eax, head
mov temp, eax

mainLoop :
call getName

lea esi, myAry
mov ch, 16
call blankout ; clear out esi

call getGrade

lea esi, myAry
call atoi
mov targetGrade, eax ; get target grade

call insertNode

mov eax, 0
mov al, [edi]
cmp al, NULL
jne mainLoop

call printLL

exit
main endp

insertNode proc
mov esi, head ; esi points to head - current node
mov prev, NULL ; prev is null

insertNodeLoop :
mov eax, NULL
cmp linky[esi], eax
je leaveInsertNodeLoop ; check if current is null

mov eax, targetGrade
cmp grade[esi], eax
jle leaveInsertNodeLoop ; check if current grade is greater than target grade

mov prev, esi ; prev equals current
lea esi, linky[esi] ; current = current->linky

jmp insertNodeLoop

leaveInsertNodeLoop :

mov lineInAddr, edi ; save line in address
mov edi, temp ; edi points to temp node

mov eax, currentNameAddr
mov name_[edi], eax ; move name into temp

mov eax, targetGrade
mov grade[edi], eax ; move grade into temp

mov linky[edi], esi ; move current address into temp

mov eax, NULL
cmp prev, eax ; cmp prev, eax
jne updatePrev ; check if we need to update prev

mov eax, edi
mov head, eax ; head equals temp
jmp leaveInsertNode

updatePrev :
mov eax, edi ; move temp into eax
mov prev + linky, eax ; prev->linky = temp mov linky[prev], eax

leaveInsertNode :

mov edi, lineInAddr ; restore linein address

add esi, 12 ; esi points to next node
mov temp, esi ; temp points to next node

ret; 4
insertNode endp

printLL proc
mov edi, head

nextNode :
mov esi, name_[edi]
call fileOut

lea esi, tab_
call fileOut ; print tab

mov eax, grade[edi]
mov ebx, 100
imul ebx
idiv maxGrade ; convert grade

call itoa
call fileOut ; print grade

lea esi, myAry
mov ch, 32
call blankout ; clear out esi

lea esi, endl
call fileOut ; print new line

mov edi, linky[edi]
cmp edi, NULL
jne nextNode

ret
printLL endp

fileOut proc
mov bl, NULL

nextChar :
mov eax, OFD
mov edx, esi
mov ecx, 1
call WriteToFile
add esi, 1

cmp[esi], bl ; check if null
jne nextChar

ret
fileOut endp

getGrade proc
lea esi, myAry
mov ebx, 0

nextDigit:
mov bl, [edi]
mov[esi], bl

inc edi
inc esi

mov bl, 0dh
cmp[edi], bl ; check for char ret
jne nextDigit

add edi, 2 ; edi points to next good char in lineIn

ret
getGrade endp

getName proc
mov esi, namesAddr ; esi points to next empty slot of names array
mov currentNameAddr, esi ; save address of current name

mov eax, 0
mov al, [edi] ; move first name char into al
and al, 223 ; convert to upper case
mov[esi], al ; move first name char into names array

getNameLoop:
inc esi
inc edi

mov al, ' '
cmp[edi], al
je leaveGetNameLoop

mov eax, 0
mov al, [edi]
mov[esi], al ; move name char into names array, the exception error is here

jmp getNameLoop

leaveGetNameLoop :

inc esi
inc edi ; edi points to next good char in lineIn

mov namesAddr, esi

ret
getName endp

atoi proc
mov eax, 0

nextDigit:
mov edx, 0
mov dl, [esi]

cmp dl, '0'
jl  outOfHere
cmp dl, '9'
jg outOfHere ; check if char is digit

add dl, -'0'

imul eax, 10

add eax, edx
inc esi

jmp nextDigit

outOfHere :

ret
atoi endp

itoa proc
itoaLoop :
mov edx, 0
mov ecx, 10

idiv ecx
add dl, '0'
dec esi
mov[esi], dl
cmp eax, 0
jne itoaLoop

ret
itoa endp

blankout proc
blnkLoop :
mov cl, ' '
mov[esi], cl
inc esi
dec ch
cmp ch, 0
jne blnkLoop

ret
blankout endp

end main

felipe

  • Member
  • *****
  • Posts: 1326
  • Eagles are just great!
Re: Program issue in MASM
« Reply #4 on: July 07, 2020, 12:29:46 AM »
You should upload the invine32.inc too.  :icon_idea:
Felipe.

felipe

  • Member
  • *****
  • Posts: 1326
  • Eagles are just great!
Re: Program issue in MASM
« Reply #5 on: July 07, 2020, 12:54:05 AM »
and the file grades.txt :icon_idea:
Felipe.

jt75

  • Regular Member
  • *
  • Posts: 12
Re: Program issue in MASM
« Reply #6 on: July 07, 2020, 01:26:05 AM »
Here's the grades.txt content. It's kind of nasty, but it has to be like that according to the directions.

300
taylor  287
Thomas     197
brown      250
lee           273
rReynolds    282
lion       192
mack         265
lewis     176
marshall    277
moto        262
vey          286
knocktosee    291
toSha       283
bozo        203
WayOut     279
ace       258
aceAgain    245
boyanze     235
thomson    188
onmore    265
ilyed     288
mark       276
beth       166
Timmy      199
cece       209
superman   293
batman     292
GreenHornit 290
ray        229
kidSid      200
kidSyd     188
lastone   245

felipe

  • Member
  • *****
  • Posts: 1326
  • Eagles are just great!
Re: Program issue in MASM
« Reply #7 on: July 07, 2020, 06:44:04 AM »
since i can't assemble this program (no ervine32.inc) can you post the .exe?  :icon_idea:
Felipe.

jj2007

  • Member
  • *****
  • Posts: 10540
  • Assembler is fun ;-)
    • MasmBasic
Re: Program issue in MASM
« Reply #8 on: July 07, 2020, 08:51:41 AM »
head dword LL
temp dword ll

What are these LL and ll?

I can assemble your code, but it keeps crashing, even with this (necessary) addition:
Code: [Select]
mov eax, 0
mov al, [edi]
cmp al, 0 ; #############
je leaveGetNameLoop
mov[esi], al ; move name char into names array, the exception error is here
deb 4, "loop", $currentNameAddr
jmp getNameLoop

jt75

  • Regular Member
  • *
  • Posts: 12
Re: Program issue in MASM
« Reply #9 on: July 07, 2020, 09:41:45 AM »
LL and ll just mean link list. Since that's where the information is supposed to go, I just named it that.

jj2007

  • Member
  • *****
  • Posts: 10540
  • Assembler is fun ;-)
    • MasmBasic
Re: Program issue in MASM
« Reply #10 on: July 07, 2020, 10:16:18 AM »
LL and ll just mean link list. Since that's where the information is supposed to go, I just named it that.

Name it LinkedList next time. The lowercase ll choked, so I had to put 0 to make it assemble. A general advice: comments please! We all know it's a nuisance having to explain what the code does, but believe me, in 3 months or so you will have difficulties understanding your own code.

jt75

  • Regular Member
  • *
  • Posts: 12
Re: Program issue in MASM
« Reply #11 on: July 07, 2020, 10:43:11 AM »
For sure, I'm trying to break that nasty habit. Assembly is definitely different.

felipe

  • Member
  • *****
  • Posts: 1326
  • Eagles are just great!
Re: Program issue in MASM
« Reply #12 on: July 08, 2020, 12:41:55 AM »
if you upload the .exe and the file grades.txt i can help you debug the code and find the problem... :icon_idea:
Felipe.

jt75

  • Regular Member
  • *
  • Posts: 12
Re: Program issue in MASM
« Reply #13 on: July 08, 2020, 03:00:01 AM »
I have the entire program and .txt file included in this post, felipe. It's a couple of posts above this one. If you're needing Irvine's library, you'll have to go to his website and install it to your Visual Studio, whichever version you're running.

jj2007

  • Member
  • *****
  • Posts: 10540
  • Assembler is fun ;-)
    • MasmBasic