Access Violation Caused After Putchar Function x86 Assembly Regardless of Call After Putchar

0

I am hoping someone may be able to help explain why I am receiving access violations when running my code below. As a disclaimer: this is a homework assignment I have been troubleshooting and working on for several days without much progress. I do not intend on turning in without first making the program my own work. Below my code gives the exception Unhandled exception at 0x77413CB3 (ntdll.dll) in [program_name].asm.exe: 0xC0000005: Access violation writing location 0x00000014. occurred After the line "call putchar". Source code below

;This programs purpose is to print out my name, [name] from it's hex values in a loop

.586 
option casemap :none
.MODEL FLAT, C
STD_OUTPUT_HANDLE EQU -11
.STACK 1024


.DATA
    nameVar db 41h, 41h, 41h, 41h, 41h, 41h, 41h, 00h; my name, [name changed for Stack overflow], in hex with the null value at the end
    i WORD ? ; iterator used to count through array of hex values

.CODE
includelib libcmt.lib
includelib libvcruntime.lib
includelib libucrt.lib
includelib legacy_stdio_definitions.lib
includelib msvcrt.lib
extrn putchar:near

main PROC
    mov esi, 0 ; set source index value as 0, used for loop below -- was: mov si, 0

PRINTLOOP:
    mov eax, 0 ; set A reg to 0 so the first letter shows properly
    mov al, nameVar [esi] ; move first value of nameinto low byte of A (accumulator) register --- was: nameVar[si]
    push eax ; push value of entire A register into the stack / this is important for printing out name hex string
    call putchar ; external function used to print out the contents of the stack into the console
    add esp, 4 ;xxxxx add esp, 8 xxxxx pop esp xxx xxx pop eax attempt to clean stack, tried the following
    inc esi ; increment the source index value after printing a character to console
    cmp esi, 7 ; hardcoding in length of my name 'nameVar' to compare length of source index vs the # of chars I'd like to print
    JE ENDPRINTLOOP ; break out of the loop if the above condition is met / si = 7 i.e my name has been printed
    jmp PRINTLOOP

ENDPRINTLOOP:

    push 0 ; push 0 value into stack

ret
main ENDP
END

I am under the impression it has to do with me writing to a location in memory that is out of bounds or read only but I cannot understand the "why" behind it. With that being said, there will be the access error immediately after the "call putchar" line, regardless of what the next line is. To me, this means that something is happening to the stack after the putchar function is called, but I do not understand what. I have looked at it in the debugger and see there is the violation shown as well, but I too new to understand how to utilize the information captured.

Any sort of tips or explanations would be greatly appreciated. Thank you. debug view

assembly
x86
masm
vs-community-edition
asked on Stack Overflow Dec 9, 2019 by gravv • edited Dec 9, 2019 by gravv

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0