The MASM Forum

Miscellaneous => Irvine Book Questions. => Topic started by: jt75 on July 06, 2020, 12:44:45 PM

Title: Program issue in MASM
Post by: jt75 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.
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
Title: Re: Program issue in MASM
Post by: hutch-- 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.
Title: Re: Program issue in MASM
Post by: TouEnMasm 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




Title: Re: Program issue in MASM
Post by: jt75 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.
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
Title: Re: Program issue in MASM
Post by: felipe on July 07, 2020, 12:29:46 AM
You should upload the invine32.inc too.  :icon_idea:
Title: Re: Program issue in MASM
Post by: felipe on July 07, 2020, 12:54:05 AM
and the file grades.txt :icon_idea:
Title: Re: Program issue in MASM
Post by: jt75 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
Title: Re: Program issue in MASM
Post by: felipe 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:
Title: Re: Program issue in MASM
Post by: jj2007 on July 07, 2020, 08:51:41 AM
Quote from: jt75 on July 06, 2020, 04:20:01 PM
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:
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
Title: Re: Program issue in MASM
Post by: jt75 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.
Title: Re: Program issue in MASM
Post by: jj2007 on July 07, 2020, 10:16:18 AM
Quote from: jt75 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.

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.
Title: Re: Program issue in MASM
Post by: jt75 on July 07, 2020, 10:43:11 AM
For sure, I'm trying to break that nasty habit. Assembly is definitely different.
Title: Re: Program issue in MASM
Post by: felipe 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:
Title: Re: Program issue in MASM
Post by: jt75 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.
Title: Re: Program issue in MASM
Post by: jj2007 on July 08, 2020, 03:23:15 AM
How do I include both windows.inc and Irvine32.inc? (http://masm32.com/board/index.php?topic=6014.msg63758#msg63758)
Title: Re: Program issue in MASM
Post by: felipe on July 08, 2020, 08:17:25 AM
Quote from: jt75 on July 08, 2020, 03:00:01 AM
If you're needing Irvine's library

No i don't. It's easier to debug the .exe program with the .txt file after downloading both from this site in a .zip file. I don't have all the time to help others. Anyway, don't worry, good luck with your homework.
Title: Re: Program issue in MASM
Post by: jt75 on July 09, 2020, 11:17:41 AM
I think this is what you're asking. I didn't really understand your meaning. Sorry about that.
Title: Re: Program issue in MASM
Post by: jj2007 on July 09, 2020, 06:53:11 PM
Your linked list has faulty entries somewhere, therefore it goes in an endless loop in printLL

I attach a debug version. To assemble the source,
- rename your version to *.old
- extract the files to the folder where you want to test it
- study the deb macro (http://www.jj2007.eu/MasmBasicQuickReference.htm#Mb1019)
- install MasmBasic (http://masm32.com/board/index.php?topic=94.0)

out     eax             252
in      eax             256
ReadFromFile    $edi            300__taylor 287__Thomas 197__brown 250__lee 273__rReynolds 282__lion 192__mack 265__lewis 176__marshall 277__moto 262__vey
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
nHornit 290__ray 229__kidSid 200__kidSyd 188__lastone 245__
getGrade        $esi            300
atoi    maxGrade        300
getNext         $edi:40         Thomas 197__brown 250__lee 273__rReynold ..
getNext         $edi:40         brown 250__lee 273__rReynolds 282__lion  ..
getNext         $edi:40         lee 273__rReynolds 282__lion 192__mack 2 ..
getNext         $edi:40         rReynolds 282__lion 192__mack 265__lewis ..
getNext         $edi:40         lion 192__mack 265__lewis 176__marshall  ..
getNext         $edi:40         mack 265__lewis 176__marshall 277__moto  ..
getNext         $edi:40         lewis 176__marshall 277__moto 262__vey 2 ..
getNext         $edi:40         marshall 277__moto 262__vey 286__knockto ..
getNext         $edi:40         moto 262__vey 286__knocktosee 291__toSha ..
getNext         $edi:40         vey 286__knocktosee 291__toSha 283__bozo ..
getNext         $edi:40         knocktosee 291__toSha 283__bozo 203__Way ..
getNext         $edi:40         toSha 283__bozo 203__WayOut 279__ace 258 ..
getNext         $edi:40         bozo 203__WayOut 279__ace 258__aceAgain  ..
getNext         $edi:40         WayOut 279__ace 258__aceAgain 245__boyan ..
getNext         $edi:40         ace 258__aceAgain 245__boyanze 235__thom ..
getNext         $edi:40         aceAgain 245__boyanze 235__thomson 188__ ..
getNext         $edi:40         boyanze 235__thomson 188__onmore 265__il ..
getNext         $edi:40         thomson 188__onmore 265__ilyed 288__mark ..
getNext         $edi:40         onmore 265__ilyed 288__mark 276__beth 16 ..
getNext         $edi:40         ilyed 288__mark 276__beth 166__Timmy 199 ..
getNext         $edi:40         mark 276__beth 166__Timmy 199__cece 209. ..
getNext         $edi:40         beth 166__Timmy 199__cece 209__superman  ..
getNext         $edi:40         Timmy 199__cece 209__superman 293__batma ..
getNext         $edi:40         cece 209__superman 293__batman 292__Gree ..
getNext         $edi:40         superman 293__batman 292__GreenHornit 29 ..
getNext         $edi:40         batman 292__GreenHornit 290__ray 229__ki ..
getNext         $edi:40         GreenHornit 290__ray 229__kidSid 200__ki ..
getNext         $edi:40         ray 229__kidSid 200__kidSyd 188__lastone ..
getNext         $edi:40         kidSid 200__kidSyd 188__lastone 245__
getNext         $edi:40         kidSyd 188__lastone 245__
getNext         $edi:40         lastone 245__
getNext         $edi:40
printing        eax             0
printing        $esi            âá@
printing        $esi            Knocktosee
printing        $esi            Taylor
printing        $esi            Taylor
printing        $esi            Taylor
printing        $esi            Taylor
printing        $esi            Taylor
printing        $esi            Taylor
printing        $esi            Taylor
----- printing done, counter=21 -----
Title: Re: Program issue in MASM
Post by: felipe on July 10, 2020, 01:39:38 AM
Great now i have the grades.txt file with the correct spacing between columns... :angelic:
I'm still willing to help you but WHERE IS THE .EXE FILE??  :undecided:
I have been pretty clear about it. Now, you started this topic in the forum saying this  :icon_idea::

Quote from: jt75 on July 06, 2020, 12:44:45 PM
the code itself compiles,

So why you can't post the .exe (attached in a .zip file, which is perfectly legal, while isn't malware of course  :icon_idea:) so i can easily debug your program and i can be able to help you?  :icon_idea: :icon_idea:
Title: Re: Program issue in MASM
Post by: jt75 on July 10, 2020, 04:25:11 PM
Alright, here's the .exe file. It's straight from my debug folder and I double checked to make sure it was the correct file you're needing.
Title: Re: Program issue in MASM
Post by: felipe on July 11, 2020, 05:17:19 AM
ok then, the program doesn't show an exception, actually it runs indefinitely creating a super big file (results.txt). This file has in it the name taylor with the grade 95 repeatedly until infinite.
I have run the program until that file reached the size of 83.135 KB, with this content:

Quote`@   97                Knocktosee             97                Taylor             95                Taylor             95...(this goes forever)

So you should check you mainloop conditions as a start... :icon_idea:
Title: Re: Program issue in MASM
Post by: jt75 on July 12, 2020, 12:45:11 AM
Any advice on how I should attack it? I just don't understand why it's doing what it's doing.
Title: Re: Program issue in MASM
Post by: jj2007 on July 12, 2020, 01:22:40 AM
printing        $esi            âá@
printing        $esi            Knocktosee
printing        $esi            Taylor


It seems the first entry of the linked list is already incorrect - see above. Do you have experience in other programming languages? I.e. is Assembly the problem, or are you generally inexperienced?
Title: Re: Program issue in MASM
Post by: felipe on July 12, 2020, 03:27:58 AM
In your printLL procedure  you are doing this:


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


But myAry is defined as this:

myAry byte 16 dup(? )


And because after myAry is defined, its defined endl like this:

endl byte 0dh, 0ah, 0


this last one gets overwritten with the spaces. So in this procedure the code that follows:

lea esi, endl
call fileOut; print new line


Doesn't do what its supposed to do. Check that out as a start  :icon_idea:.

Here is a more complete picture of the above:


.data
...
myAry byte 16 dup(? )
endl byte 0dh, 0ah, 0
tab_ byte 09h, 0
...

.code
...
printLL proc

...

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

lea esi, endl
call fileOut; print new line
...

printLL endp



:icon_idea:
Title: Re: Program issue in MASM
Post by: jt75 on July 12, 2020, 05:54:51 AM
Thanks guys, I'll check that out and see what I can do. Yeah I'm a very inexperienced programmer and this is really my first semester dealing with assembly, it's been extremely hard to grasp.
Title: Re: Program issue in MASM
Post by: jt75 on July 12, 2020, 06:48:34 AM
I made changes to the code in the LL proc, where the array was declared as 16, in the mov ch, I changed it from 32 to 16 and that has the output file printing up and down now instead of straight across, so that much is remedied. I'm still trying to figure out what to do to the endl in that same procedure. I know it needs to go to the next name and print the grade until it runs out of data, but that's been my biggest issue ever since I started getting back results. Originally the exception error was due to my faulty .txt file which I did correct. Now it's just normal output I need to get corrected.

lea esi, myAry
mov ch, 16
call blankout; clear out esi
Title: Re: Program issue in MASM
Post by: felipe on July 12, 2020, 07:11:55 AM
can you post the command line or the .bat file you use to assemble and link your project? (i have no time to figure it out... :icon_idea:)
Title: Re: Program issue in MASM
Post by: jt75 on July 12, 2020, 08:11:27 AM
I don't see a .bat file within my folders, I'm just using VS 2015 with Irvine's addons if that helps.
Title: Re: Program issue in MASM
Post by: felipe on July 12, 2020, 08:27:01 AM
I think you should now check the procedure insertNode  :icon_idea: