News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

GENERAL PROTECTION FAULT - Why?

Started by frktons, December 14, 2012, 05:30:07 AM

Previous topic - Next topic

frktons

After displaying correctly:

Quote
label_a
label_b

the program freezes and gives a General Protection Fault.
I don't see an apparent reason. Any help?

Frank


.nolist
include \masm32\include\masm32rt.inc
.486


.code
start:

    align 4
    mov eax, label_a
    mov ebx, label_b
    mov ecx, label_c
    mov edx, label_d

    jmp eax
   
align 4
label_c:

    print " label_c", 13, 10
    jmp edx

align 4
label_a:

    print " label_a", 13, 10
    jmp ebx

align 4
label_d:

    print " label_d", 13, 10
    jmp end_test

align 4
label_b:

    print " label_b", 13, 10
    jmp ecx

align 4
end_test:

    inkey chr$(13, 10, "--- ok ---", 13)
    exit


end start

There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

frktons

Probably the print macro modifies the GPRS.
Maybe I've to change something... ::)
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

dedndave

it destroys EAX, ECX, and EDX (EAX is ok to destroy in your case)
in general, you can assume that masm32 lib and API functions preserve EBX, EBP, ESI, EDI

frktons

Quote from: dedndave on December 14, 2012, 05:49:38 AM
it destroys ECX
in general, you can assume that masm32 lib and API functions preserve EBX, EBP, ESI, EDI

I tried to push ecx before calling print macro but t didn't work.
I had to use a workaround to make it run:

    align 4
    mov eax, label_a

    jmp eax
   
align 4
label_c:

    print " label_c", 13, 10
    mov edx, label_d   
    jmp edx

align 4
label_a:

    print " label_a", 13, 10
    mov ebx, label_b   
    jmp ebx

align 4
label_d:

    print " label_d", 13, 10
    jmp end_test

align 4
label_b:

    print " label_b", 13, 10
    mov ecx, label_c   
    jmp ecx

align 4
end_test:


But it is not the preinitialization I was looking for ::)
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

dedndave

also - EDX   :biggrin:

you have 4 labels - and you do not need to conform to the ABI
so, use EBX, EBP, ESI, and EDI   :t

i try to keep EBP free for locals and the first one executed does not need to be preserved
so, for that one, use EAX, ECX, or EDX

frktons

Quote from: dedndave on December 14, 2012, 05:56:30 AM
also - EDX   :biggrin:

you have 4 labels - and you do not need to conform to the ABI
so, use EBX, EBP, ESI, and EDI   :t

i try to keep EBP free for locals and the first one executed does not need to be preserved
so, for that one, use EAX, ECX, or EDX

OK Dave, but I found a solution that suits my needs, I need esi and edi free:

align 4
    mov eax, label_a
    mov ebx, label_b
    mov ecx, label_c
    mov edx, label_d

    push edx
    push ecx
    push ebx
   
    jmp eax
   
align 4
label_c:

    print " label_c", 13, 10
    pop edx
    jmp edx

align 4
label_a:

    print " label_a", 13, 10
    pop ebx
    jmp ebx

align 4
label_d:

    print " label_d", 13, 10
    jmp end_test

align 4
label_b:

    print " label_b", 13, 10
    pop ecx
    jmp ecx

align 4
end_test:

:P
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

dedndave

.nolist
include \masm32\include\masm32rt.inc
.486

.code

align 4
start:

    push  label_d
    push  label_c
    push  label_b
    push  label_a
    retn
   
align 4
label_c:
    print " label_c", 13, 10
    retn

align 4
label_a:
    print " label_a", 13, 10
    retn

align 4
label_d:
    print " label_d", 13, 10
    jmp end_test

align 4
label_b:
    print " label_b", 13, 10
    retn

align 4
end_test:
    inkey chr$(13, 10, "--- ok ---", 13)
    exit

end start

frktons

Interesting Dave  :icon_exclaim:

So with retn I pop a value and jump there, as it looks like.
I never used this solution. Thanks for the hint  :t
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

frktons

And, as usual here it is your note:

http://masm32.com/board/index.php?action=dlattach;topic=1046.0;attach=874;image
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

dedndave

i have seen another method that might interest you, depending on what you are doing
that is to use "command lists"
the beauty of this is that you can make different lists for different uses
there are numerous variations on this, but here is the basic idea....

.DATA

list1 dd label_a,label_b,label_c,label_d,label_c,label_b,label_a,end_test

.CODE

start:
    mov ebx,offset list1
    jmp dword ptr [ebx]

align 4
label_c:
    add ebx,4
    print " label_c", 13, 10
    jmp dword ptr [ebx]

align 4
label_a:
    add ebx,4
    print " label_a", 13, 10
    jmp dword ptr [ebx]

align 4
label_d:
    add ebx,4
    print " label_d", 13, 10
    jmp dword ptr [ebx]

align 4
label_b:
    add ebx,4
    print " label_b", 13, 10
    jmp dword ptr [ebx]

align 4
end_test:
    inkey chr$(13, 10, "--- ok ---", 13)
    exit

end start


EDIT - added "dword ptr" to the branches

dedndave

if you wanted to, you could put the string pointers in the list and step by 8   :P

another way to go is to have a single dword variable that holds a branch vector
each step in the code can modify the vector for the next branch

frktons

Quote from: dedndave on December 14, 2012, 06:22:47 AM
i have seen another method that might interest you, depending on what you are doing
that is to use "command lists"
the beauty of this is that you can make different lists for different uses
there are numerous variations on this, but here is the basic idea....

.DATA

list1 dd label_a,label_b,label_c,label_d,label_c,label_b,label_a,end_test

.CODE

start:
    mov ebx,offset list1
    jmp dword ptr [ebx]

align 4
label_c:
    add ebx,4
    print " label_c", 13, 10
    jmp dword ptr [ebx]

align 4
label_a:
    add ebx,4
    print " label_a", 13, 10
    jmp dword ptr [ebx]

align 4
label_d:
    add ebx,4
    print " label_d", 13, 10
    jmp dword ptr [ebx]

align 4
label_b:
    add ebx,4
    print " label_b", 13, 10
    jmp dword ptr [ebx]

align 4
end_test:
    inkey chr$(13, 10, "--- ok ---", 13)
    exit

end start


EDIT - added "dword ptr" to the branches

Yes it is very interesting. Maybe it is called jump list and
I considered it as a valid alternative to switch/case and the like.

If there are multiple choices with jumps as results of the choice
then it is the perfect faster alternative to slow compare opcodes.

At least this is what I foresee looking at the code.  :icon_eek:
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

dedndave

it implements a little cleaner if you use ESI and LODSD
but, you said you wanted to ues ESI for something else

frktons

Quote from: dedndave on December 14, 2012, 10:30:06 AM
it implements a little cleaner if you use ESI and LODSD
but, you said you wanted to ues ESI for something else

It doesn't mean I can't change my mind  :lol:
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

dedndave

INCLUDE \masm32\include\masm32rt.inc

.DATA

list1 dd label_a,label_b,label_c,label_d,label_c,label_b,label_a,end_test

.CODE

start:
    mov esi,offset list1
    lodsd
    jmp     eax

align 4
label_c:
    print " label_c", 13, 10
    lodsd
    jmp     eax

align 4
label_a:
    print " label_a", 13, 10
    lodsd
    jmp     eax

align 4
label_d:
    print " label_d", 13, 10
    lodsd
    jmp     eax

align 4
label_b:
    print " label_b", 13, 10
    lodsd
    jmp     eax

align 4
end_test:
    inkey chr$(13, 10, "--- ok ---", 13)
    exit

end start