Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XOR statement in assembly [duplicate]

I am trying to understand the disassembled version of this program:

#include <stdio.h>

int main(){
   int i=0;
   printf("HELLO VIK");
   return 0;
}

gdb disassembly:

(gdb) disass main
Dump of assembler code for function main:
0x0000000100000ef0 <main+0>:    push   rbp
0x0000000100000ef1 <main+1>:    mov    rbp,rsp
0x0000000100000ef4 <main+4>:    sub    rsp,0x10
0x0000000100000ef8 <main+8>:    mov    DWORD PTR [rbp-0xc],0x0
0x0000000100000eff <main+15>:   xor    al,al
0x0000000100000f01 <main+17>:   lea    rcx,[rip+0x50]        # 0x100000f58
0x0000000100000f08 <main+24>:   mov    rdi,rcx
0x0000000100000f0b <main+27>:   call   0x100000f2c <dyld_stub_printf>
0x0000000100000f10 <main+32>:   mov    DWORD PTR [rbp-0x8],0x0
0x0000000100000f17 <main+39>:   mov    eax,DWORD PTR [rbp-0x8]
0x0000000100000f1a <main+42>:   mov    DWORD PTR [rbp-0x4],eax
0x0000000100000f1d <main+45>:   mov    eax,DWORD PTR [rbp-0x4]
0x0000000100000f20 <main+48>:   add    rsp,0x10
0x0000000100000f24 <main+52>:   pop    rbp
0x0000000100000f25 <main+53>:   ret

If I understand the first 3 lines correctly, the base pointer is being pushed to the stack as the return address. Then the base pointer is set to the current stack pointer. The size of the stack is set to 16 bytes (x10). The size of the int i is 12 bytes(0xc) and is set to 0. I'm not sure what (xor al, al) does. Did i interpet this correctly? What does the xor al, al line do?

like image 212
vikash dat Avatar asked Dec 06 '25 09:12

vikash dat


1 Answers

xor al,al is a quick way to zero out a register. It's a one-byte opcode in x86 assembler, v.s. 2bytes for mov al, 0.

like image 170
Marc B Avatar answered Dec 08 '25 04:12

Marc B