0

i have the following code in assembly:

.686
.model flat, stdcall
.stack 4096
ExitProcess proto, dwExitCode : dword

.data
    Temporal DWORD ?
    mdc_result DWORD ?

.code
    main PROC
        push ebp
        mov ebp, esp 

        push 120
        push 25
        call MCD_iterativo

        pop ebp
        ret 0

    main ENDP

    MCD_iterativo PROC ; a = 4, b = 8
        push ebp
        mov ebp, esp
        jmp L2

        pop ebp
        ret 
    MCD_iterativo ENDP

    L2:
        cmp DWORD PTR [ebp + 8], 0
        je L1

        mov eax, DWORD PTR [ebp + 8]
        mov DWORD PTR Temporal, eax 

        mov eax, DWORD PTR [ebp + 12]
        cdq
        idiv DWORD PTR [ebp + 8]
        mov DWORD PTR [ebp + 8], edx

        mov ecx, DWORD PTR Temporal
        mov DWORD PTR [ebp + 12], ecx

        jmp L2

    L1:
        mov eax, DWORD PTR [ebp + 12]
        pop ebp
        ret 0

END main

I'm getting the following error: Exception occurred at 0x00000005 in ASM_Miercoles_Semana10.exe: 0xC0000005: Access violation when executing location 0x00000005. I tried making an add into the esp and ebp variable to recover the initial address but it doesnt know, i just want to finish the program without errors I'm trying to implement two functions and the second function have a while inside of it, the loop is working well, i have the error at the end of all, in the ret of main PROC

6
  • 1
    Looks like a near duplicate of Segmentation fault when pushing on stack (NASM) - call MCD_iterativo doesn't pop the args you pushed, so you eventually ret and pop 125 into EIP. IDK why you'd be getting an error at 0x00000005 instead of 0x7d (125 in hex). Oh, the code inside MCD_iterativo / L2 modifies its stack arg (which is fine), so you're returning to GCD(5,120) = 5. No need to store/reload anything in that function; looks like un-optimized compiler output. Commented Oct 28, 2022 at 3:37
  • 1
    I thought .model flat, stdcall might make MASM magically turn ret 0 into ret 8, but maybe an explicit ret 0 overrides that. And you didn't tell MASM about the args the function takes, just a bare proc, which is fine it just means that MASM can't magically change it into a stdcall function that pops the stack as it returns. Commented Oct 28, 2022 at 3:42
  • 2
    @PeterCordes : While that is true about ret 0, there is a bigger issue. MASM doesn't know how many parameters MCD_iterativo PROC has because no parameters have been specified (so doesn't know it should be ret 8). Example: MCD_iterativo PROC arga:DWORD, argb:DWORD . That would change ret to ret 8. That only applies to the code inside the PROC . He jumps to L2 outside the PROC so that ret will not be changed to ret 8. As well once you specify at least one argument to MCD_iterativo there is no need to do the stack prologue and epilogue since they would be done for you. Commented Oct 28, 2022 at 4:13
  • 1
    Speculative: the strange jump to L2 outside the PROC might have been an attempt to fix the stack issue but that method failed. Commented Oct 28, 2022 at 4:18
  • 2
    @MichaelPetch: Thanks. Unclear whether the OP wants MASM to do stack cleanup for them, or whether they were just unaware of it as a possible problem. If you know a good canonical duplicate about MASM magically doing stdcall for you, you could add it to the duplicate list. Good point about needing to remove the manual EBP setup if MASM does it for you, otherwise the offset would be wrong. Commented Oct 28, 2022 at 4:25

1 Answer 1

1

The best Solution would be to debug your code - in Visual Studio. Basically, your code between Loops [L1,L2] at some point gets out of the bound of the program stack and enters the restricted area of the OS. When this happens - the OS crashes the program as you are not allowed to access that information.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.