2012-11-05

Start from helloworld.asm





asm_notes.org




* asm
** gdb:::
resource                ::$ http://www.chemie.fu-berlin.de/chemnet/use/info/gdb/gdb_toc.html
stop at main            ::$ b main | r
view register           ::info registers
show single reg         ::print/(d|t|x) $eax
show memory value       ::x/<num>(c|d|x)(b|h|w)    x/42cb &output
(gdb) x/s $edi+28
0x600104 <output+28>:    "GenuineIntel'\n"
** common 
Code of helloworld.asm::        
SECTION .data
EatMsg: db "hello world", 10
EatLen: equ $-EatMsg
SECTION .bss
SECTION .text
global _start
_start:
        nop
        mov eax, 4
        mov ebx, 1
        mov ecx, EatMsg
        mov edx, EatLen
        int 80H

        mov eax, 1
        mov ebx, 0
        int 80H
Command to build::
[vagrant@vagrant-centos-6-64 vagrant]$ nasm -f elf -g -F stabs helloworld.asm 
helloworld.asm  helloworld.o
[vagrant@vagrant-centos-6-64 vagrant]$ ld -o helloworld helloworld.o -melf_i386
helloworld  helloworld.asm  helloworld.o
[vagrant@vagrant-centos-6-64 vagrant]$ ./helloworld 
hello world

dump all sections       ::$ objdump -D eatsyscall
08048080 <_start>:      ||break *_start+1 then %epi will contains 0x08048080
 8048080:       90                      nop
 8048081:       b8 04 00 00 00          mov    $0x4,%eax
 8048086:       bb 01 00 00 00          mov    $0x1,%ebx
gcc output              ::$ gcc -S ctest.c
gcc -E                  ::$ gcc -E ctest.c
compile wit debug info  ::$ gcc -gstabs -gp -o ctest ctest.c 
dump code with assembly ::$ objdump -S ctest
int foo()
{
  400684:       55                      push   %rbp
  400685:       48 89 e5                mov    %rsp,%rbp
  400688:       48 83 ec 10             sub    $0x10,%rsp
  40068c:       e8 a7 fe ff ff          callq  400538 <mcount@plt>  ||<--insert by -gp for gprof

#include <stdio.h>
int main()
{
  400504:       55                      push   %rbp
  400505:       48 89 e5                mov    %rsp,%rbp
  printf("Hello, World!\n");
  400508:       bf 18 06 40 00          mov    $0x400618,%edi
  40050d:       e8 de fe ff ff          callq  4003f0 <puts@plt>
  exit(0);
  400512:       bf 00 00 00 00          mov    $0x0,%edi
  400517:       e8 e4 fe ff ff          callq  400400 <exit@plt>
dump out dynamic symbol table entries::$ objdump -T ctest
ctest:     file format elf64-x86-64
DYNAMIC SYMBOL TABLE:
0000000000000000  w   D  *UND*  0000000000000000              __gmon_start__
0000000000000000      DF *UND*  0000000000000000  GLIBC_2.2.5 puts
0000000000000000      DF *UND*  0000000000000000  GLIBC_2.2.5 exit
0000000000000000      DF *UND*  0000000000000000  GLIBC_2.2.5 __libc_start_main
dump dynamic entries::$ readelf -d cpuid2
Dynamic section at offset 0x2b0 contains 15 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
show used share libs::$ ldd cpuid2
        libc.so.6 => /lib64/libc.so.6 (0x00007f0e83eb4000)
        /lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x00007f0e8424f000)
mov:: movl(long word) movw(16-bit word) movb(byte)  | movl %eax, %ebx | movw %ax %bx | movb %al %bh
conditional move:: movl value, %ecx | cmp %ebx, %ecx | cmova %ebx, %ecx
XCHG::exchange value between two general purpos registers, or betwen a register and a memory location.
when one of the operands is a memory location, the processor's LOCK signal is automatically asserted,
to turns the instruction into an atomic instruction in multiple processor environment.
CMPXCHG:: cmpxchg src dest | compare dest with EAX,AX,or AL. if equal, load src to dest. 
otherwise load dest to EAX, AX, or AL.  
unconditional branches::jump calls interrupts
call: 1) push the $eip, modifies the %eip to point to called function address 
      2) call function and ret 
      3) pop %eip to continue
asm function template::
function_label:
    pushl %ebp
    movl %esp, %ebp
    <normal function code>
    movl %ebp, %esp
    popl %ebp

software interrupts::are provided by OS to enable app to tap info function into the OS. (system calls).

** resources
http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html

-->