Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

do all variables are automatically get stored in starting with rax register as code execute as stuff start happening on variables

I have a simple C program that I assume generate simple assembly code

this is my C program

char *a="ba";
char x;

void main(){x=a[0];}

There is nothing especial about the generated assembly except

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

these lines

# test.c:4: void main(){x=a[0];}
    movq    a(%rip), %rax   # a, a.0_1
    movzbl  (%rax), %eax    # *a.0_1, _2
# test.c:4: void main(){x=a[0];}
    movb    %al, x(%rip)    # _2, x

so I am in the middle of my program assembly and all the sudden quad word worth of address is moved happens to be from rax to rip, so rip is instruction pointer so I like to know what is inside rax at following line

    movq    a(%rip), %rax   # a, a.0_1

does it hold my a[0], if it does that this makes sense because this line test.c:4: void main(){x=a[0];} movb %al, x(%rip) # _2, x so this mean my rax hold every element inside char *a and least significant type will be a[0] but I dont understood how come rax start holding my char *a I did not assign it then who assigned my char *a to rax register so I am assuming al there will be a[0] and a[1] will be at 8th to 15th byte of rax register. And what about this line movzbl (%rax), %eax # *a.0_1, _2 whats doing here

this is almost full assembly with disassembler directives

    .text
    .globl  a
    .section    .rodata
.LC0:
    .string "ba"
    .section    .data.rel.local,"aw"
    .align 8
    .type   a, @object
    .size   a, 8
a:
    .quad   .LC0
    .globl  x
    .bss
    .type   x, @object
    .size   x, 1
x:
    .zero   1
    .text
    .globl  main
    .type   main, @function
main:
    pushq   %rbp    #
    movq    %rsp, %rbp  #,

# test.c:4: void main(){x=a[0];}
    movq    a(%rip), %rax   # a, a.0_1
    movzbl  (%rax), %eax    # *a.0_1, _2
# test.c:4: void main(){x=a[0];}
    movb    %al, x(%rip)    # _2, x
# test.c:4: void main(){x=a[0];}
    nop 
    popq    %rbp    #
    ret 

>Solution :

 movq    a(%rip), %rax

moves the value of a, which is the address of the string, into rax.

 movzbl  (%rax), %eax

moves a[0] into rax. (This reads one byte.)

 movb    %al, x(%rip)

moves the value of a[0] into x.

The first and third of these instructions mention rip because they use rip-relative addressing, which is typical for accessing static variables in 64-bit code.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading