December 11, 2017

Assembly code(printf, scanf, while, for, if, switch, variable, pointer, array)

※ Basic Assembly instruction set(Link).


❑ C code : return

return 0;

❑ Assembly code : return
mov eax,0x0



❑ C code : variable
int a1=1;
int a2=2;
int a3=3;
int a4=4;
int a5=5;

❑ Assembly code : variable
sub esp,0x20
mov DWORD PTR [ebp-0x14],0x1
mov DWORD PTR [ebp-0x10],0x2
mov DWORD PTR [ebp-0xc],0x3
mov DWORD PTR [ebp-0x8],0x4
mov DWORD PTR [ebp-0x4],0x5




❑ C code : array, pointer
char ary1[] = "hello world";
printf("%s", ary1);

char *ptr1 = "hello world";
printf("%s",ptr1);

❑ Assembly code : array, pointer
<main+28> mov DWORD PTR [ebp-0x1a],0x6c6cxxxx
<main+35> mov DWORD PTR [ebp-0x16],0x6f57xxxx
<main+42> mov DWORD PTR [ebp-0x12],0x2164xxxx
<main+49> mov WORD PTR [ebp-0xe],0xa
<main+55> sub esp,0xc
<main+58> lea eax,[ebp-0x1a]
<main+61> push eax
<main+62> call 0x8048340 <puts@plt>
<main+67> add esp,0x10

<main+70> mov DWORD PTR [ebp-0x20],0x8048xxx
<main+77> sub esp,0xc
<main+80> push DWORD PTR [ebp-0x20]
<main+83> call 0x8048340 <puts@plt>

Array divides the string for inputing it to the stack and puts the start address of the array as an argument.

Pointer inputs the string to the stack at once and puts the address of the pointer as an argument.



❑ C code : printf
printf("abcde");

int abc=16;
printf("%d", abc);

❑ Assembly code : printf
sub esp,0x10
mov DWORD PTR [esp],0x8048xxx
call 0x8048xxx <printf@plt>

sub esp,0x20
mov DWORD PTR [esp+0x1c],0x10
mov eax,DWORD PTR [esp+0x1c]
mov DWORD PTR [esp+0x4],eax → 16
mov DWORD PTR [esp],0x8048xxx → "%d"
call 0x8048xxx <printf@plt>



❑ C code : scanf
int abc;
scanf("%d", &abc);

❑ Assembly code : scanf
sub esp,0x20
lea eax,[esp+0x1c]
mov DWORD PTR [esp+0x4],eax → &abc
mov DWORD PTR [esp],0x8048xxx → "%d"
call 0x8048xxx <__isoc99_scanf@plt>



❑ C code : while, for
while(abc<=16) {
  abc++;
}

for(;abc<=16;abc++) {
}

❑ Assembly code : while, for
<main+20> jmp 0x8048xxx <main+26>
<main+22> add DWORD PTR [esp+0x1c],0x1
<main+26> cmp DWORD PTR [esp],0x10
<main+29> jle 0x8048xxx <main+22>



❑ C code : if
if(abc==1)
  printf("abc is one");
else if(abc==2)
  printf("abc is two");
return 0;

❑ Assembly code : if
<main+20> mov eax,DWORD PTR [esp+0x1c]
<main+24> cmp eax,0x1
<main+27> jne 0x8048xxx <main+43>
<main+29> mov DWORD PTR [esp],0x8048xxx
<main+36> call 0x8048xxx <printf&plt>
<main+41> jmp 0x8048xxx <main+80>
<main+43> mov eax,DWORD PTR [esp+0x1c]
<main+47> cmp eax,0x2
<main+50> jne 0x8048xxx <main+80>
<main+52> mov DWORD PTR [esp],0x8048xxx
<main+59> call 0x8048xxx <printf&plt>
<main+64> jmp 0x8048xxx <main+80>
<main+…> ~~~~~~~~~~~~~~~~~~
<main+80> mov eax,0x0



❑ C code : switch
switch(abc){
  case 1:
  printf("abc is one");
  case 2:
  printf("abc is two");
}
return 0;

❑ Assembly code : switch
<main+20> mov eax,DWORD PTR [esp+0x1c]
<main+24> cmp eax,0x1
<main+27> jne 0x8048xxx <main+43>
<main+29> mov DWORD PTR [esp],0x8048xxx
<main+36> call 0x8048xxx <printf&plt>
<main+41> jmp 0x8048xxx <main+80>
<main+43> cmp eax,0x2
<main+46> jne 0x8048xxx <main+80>
<main+48> mov DWORD PTR [esp],0x8048xxx
<main+55> call 0x8048xxx <printf&plt>
<main+60> jmp 0x8048xxx <main+80>
<main+…> ~~~~~~~~~~~~~~~~~~
<main+80> mov eax,0x0

Switch statement puts the value to be compared into eax only once.

If statement puts the value to be compared into eax each time when the value should be compared.