>>101092530
Today I decided to see whether I could try writing windows x64 assembly on a whim, but on doing so I encountered a problem I cant seem to wrap my head around.
The following is compiled through the VS2022 IDE, if that helps.
// main.cpp
constexpr size_t memory_size = 0xFFFF;
uint8_t memory[memory_size];
extern "C" void update(uint8_t *, size_t);
int main() {
printf("before: %d\n", memory[0]);
update(memory, memory_size);
printf("after: %d\n", memory[0]);
return 0;
}
; update.asm
.data
update proc
mov r8b, byte ptr [rcx] ; Segfaults while trying to execute this line
add r8b, 2
mov byte ptr [rcx], r8b
ret
update endp
end
To try my hand at diagnosing why this segfaulted, I tried inspecting the disassembly
// main.cpp
*memory += 2;
; disassembly.asm
mov eax,1
imul rax,rax,0
lea rcx,[memory (07FF70A6AC000h)]
movzx eax,byte ptr [rcx+rax]
add eax,2
mov ecx,1
imul rcx,rcx,0
lea rdx,[memory (07FF70A6AC000h)]
mov byte ptr [rdx+rcx],al
; simplified.asm
; Comments are my interpretation of what should be happening
; read byte
lea rcx, [memory]
movzx eax, byte ptr [rcx]
; add byte
add eax, 2
; write byte
lea rdx, [memory]
mov byte ptr [rdx], al
As far as I can tell, this is essentially my original code minus some extra loads and stores but I still dont get why my code segfaults.