Moving register values to memory variables in section data
I need help moving a value from a register to memory but every time I run my code I get a seg fault. The goal is to assign M to J + K - 1.
section data: M dw 0 J dw 3 K dw 4 section text: global _start ; Show how this statement M= J+K -1 could be translated into assembly code using 8086 instruction set. ; Assume M, J and K are memory variables. In 8086 assume 16-bit, we can use MOV ; instruction to copy the variable into register ex: MOV AX, J. _start: mov bx, [K] ; move K into bx sub bx, 1 ; subtract 1 from bx mov ax, [J] ; move J into ax add ax, bx ; add J + (K - 1) mov [M], ax ; move ax value into M. This is where the seg fault occurs. mov rax, 60 mov rdi, 0 syscall
The linker doesn't know about sections called
text:, they're just random custom section names and you didn't set permission (read/write/execute) for them. (Use
:after labels, not section names)
(Also, I'd recommend
default relbecause you want NASM to use RIP-relative addressing for addresses like
After building a static executable with
nasm -felf64 foo.asm && ld -o foo foo.oon my Arch Linux desktop,
$ readelf -a foo ... Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flags Align LOAD 0x0000000000001000 0x0000000000401000 0x0000000000401000 0x0000000000000031 0x0000000000000031 R 0x1000 Section to Segment mapping: Segment Sections... 00 data: text: ...
So we can see that the
data:sections were both linked a read-only non-executable program segment, so code-fetch for the first instruction of
_startwill fault. Or at least you'd expect it would, but single-stepping under GDB it didn't segfault until it tried to store back to memory, and that faulted because it is mapped read-only.
And yes, that the
:at the end of the section names did actually appear in the object file.