Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NASM declaring variable in .text section

Tags:

assembly

nasm

I just started learning asm with nasm and I noticed that my code works perfectly fine if I declare a variable in my .text section. Just wondering why I can do this and how it is different from using the .data section. Also I noticed that I need a : after the variable names in the .bss section but not in the .data section. Why is this?

section .text
    global _start

    _start:
        var: db "xyzzzzz"


        mov eax, 4
        mov ebx, 1
        mov ecx, var
        mov edx, 4
        int 0x80

        mov eax, 1
        int 0x80

section .bss
    out: resb 1
    out2: resb 1



    4000b0: 78 79                   js     40012b <_start+0x7b>
  4000b2:   7a 7a                   jp     40012e <_start+0x7e>
  4000b4:   7a 7a                   jp     400130 <_start+0x80>
  4000b6:   7a b8                   jp     400070 <_start-0x40>
  4000b8:   04 00                   add    $0x0,%al
  4000ba:   00 00                   add    %al,(%rax)
  4000bc:   bb 01 00 00 00          mov    $0x1,%ebx
  4000c1:   b9 b0 00 40 00          mov    $0x4000b0,%ecx
  4000c6:   ba 04 00 00 00          mov    $0x4,%edx
  4000cb:   cd 80                   int    $0x80
  4000cd:   b8 01 00 00 00          mov    $0x1,%eax
  4000d2:   cd 80                   int    $0x80

I also ran it through objdump. Also it seems to have js and jp commands. Do these tell the cpu to skip over the data so that it will not execute my string?

like image 379
chasep255 Avatar asked Oct 19 '25 10:10

chasep255


1 Answers

As Jester correctly said, .text section are generally read only and meant for executable code only.

On IA32e you can use it as a read-only area but it is possible for some architecture (for example Harvard1 ones) to have code and data on different address space with no instructions for reading data from the code address space.

Also nothing forbid an architecture from having pages of memory with execute only access right, thus preventing reading but not fetching on that areas.

1If you like to experiment, you can desync the iTLB and dTLB on IA32e to simulate an Harvard machine!


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!