0

我希望有人能够帮助解释为什么我在下面运行我的代码时会收到访问冲突。作为免责声明:这是一项家庭作业,我已经进行了几天的故障排除和工作,但没有太大进展。我不打算在没有先把程序变成我自己的工作的情况下上交。下面我的代码给出了异常Unhandled exception at 0x77413CB3 (ntdll.dll) in [program_name].asm.exe: 0xC0000005: Access violation writing location 0x00000014。发生在“call putchar”行之后。源代码如下

;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

我的印象是它与我写入内存中超出范围或只读的位置有关,但我无法理解其背后的“原因”。话虽如此,无论下一行是什么,在“call putchar”行之后都会立即出现访问错误。对我来说,这意味着调用 putchar 函数后堆栈发生了一些事情,但我不明白是什么。我在调试器中查看了它,发现也显示了违规,但我太新了,无法理解如何利用捕获的信息。

任何类型的提示或解释将不胜感激。谢谢你。 调试视图

4

0 回答 0