1

Using YASM I have tried to reserve space for 2000 quadwords, but when I do this I get a SIGSEGV when I try to write into the reserved block of quadwords. If I reserve space for only 300 quadwords, the program runs without error. What causes this?

; Using Windows 7 (Intel Celeron 64-bits)
; yasm -f win64 -l forth.lst forth.asm
; gcc -o forth forth.obj

segment .data
controlstr db "%x", 13, 10, 0

segment .bss
dictionaryspace resq 2000
datastackspace  resq 300
databottom  dq 0
returnstackspace resq 300
returnbottom dq 0

segment .text
global main
extern printf

main:
push rbp                ; setup stack frame
mov rbp, rsp
sub rsp, 32             ; reserve space

lea r15, [databottom]   ; initialize data stack pointer
sub r15, 8              ; point to the last word in data stack

mov rax, 666
mov [r15], rax          ; SIGSEGV happens here.

mov rdx, [r15]
lea rcx, [controlstr]
call printf

leave
ret

; End of code.
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Patrick Clot
  • 111
  • 2
  • unrelated: `lea r15, [databottom-8]` should be equivalent and shorter. Does `default rel` make any difference? Using LEA with an absolute `[disp32]` addressing mode should work. BTW, if you're on Windows, you don't *literally* get SIGSEGV do you? I thought Windows didn't use POSIX signals, unless you're using the Linux subsystem for Windows, but you're using `-fwin64` not `elf64`. – Peter Cordes Jun 29 '18 at 00:28
  • Also, if you don't need the value in a register, you can `mov qword [r15], 666`. – Peter Cordes Jun 29 '18 at 00:30
  • Why are you using `dq 0` instead of `resq 1`? And if you just want a label past the end of `datastackspace`, use `databottom:` on a line by itself. (Labels are zero-width). You should always use `:` after label names in NASM syntax, to avoid ambiguity with instruction mnemonics. Anyway, I don't see any reason why your code would fault on the store; so presumably the problem is specific to Windows or something about your build environment. It runs fine on Linux (modulo calling-convention arg-passing register differences.) – Peter Cordes Jun 29 '18 at 00:33
  • What development environment on Windows are you using? Msys? Msys2? Cygwin? something else? – Michael Petch Jun 29 '18 at 01:51
  • Works fine with mingw and wine. – Jester Jun 29 '18 at 02:03
  • I am using mingw. Maybe I should try a different version of gcc/yasm. – Patrick Clot Jun 30 '18 at 23:34

0 Answers0