1. Use g++ to assembly. Downloaded form Mingw (under personal distribution diretory, find the 64 bit version)

 

C++ source code and assembly code before link 1

C code:

 

#include <stdio.h>

 

void pp( const char *a,

const char *b,

int c1,

int c2,

int c3,

int c4,

int c5,

int c6,

int c7,

int c8);

 

int main () { 

pp("first string\n","second string\n", 1, 2, 3, 4, 5, 6, 7, 8);

return(0);

}

 

void pp( const char *a,

const char *b,

int k1,

int k2,

int k3,

int k4,

int k5,

int k6,

int k7,

int k8) {

printf(a);

printf(b);

k1=k1+k2+k3+k4+k5+k6+k7+k8;

printf("%d", k1);

}

 

 

 

Assembly code:

 

; ------------------------------------------------Starts from here 

.file "main.cpp"

.def __main; .scl 2; .type 32; .endef

.section .rdata,"dr"

.LC0:

.ascii "second string\12\0"

.LC1:

.ascii "first string\12\0"

.text

.globl main

.def main; .scl 2; .type 32; .endef

 

; ------------------------------------------------Entry point

main:

pushq %rbp      ;create stack

movq %rsp, %rbp         ;10 parameters * 8 bytes per parameter

subq $80, %rsp       ;total 0x80 bytes of stack

call __main

movl $8, 72(%rsp)        ;default parameters passing

movl $7, 64(%rsp)        ;RCX, RDX, R9D, R8D, and then stack.

movl $6, 56(%rsp)

movl $5, 48(%rsp)

movl $4, 40(%rsp)

movl $3, 32(%rsp)

movl $2, %r9d

movl $1, %r8d

leaq .LC0(%rip), %rdx

leaq .LC1(%rip), %rcx

call _Z2ppPKcS0_iiiiiiii

movl $0, %eax              ;main return is still using eax

leave

ret

 

.section .rdata,"dr"

.LC2:

.ascii "%d\0"                 ;local raw data used by printf inside subroutine.

.text

.globl _Z2ppPKcS0_iiiiiiii

.def _Z2ppPKcS0_iiiiiiii; .scl 2; .type 32; .endef

 

_Z2ppPKcS0_iiiiiiii:

pushq %rbp

movq %rsp, %rbp

subq $32, %rsp

movq %rcx, 16(%rbp)        ;First 4 parameters preserved to local stack

movq %rdx, 24(%rbp)

movl %r8d, 32(%rbp)

movl %r9d, 40(%rbp)

movq 16(%rbp), %rcx        ;pass through rcx

call printf

movq 24(%rbp), %rax        ;pass through rcx, but why bother rax?

movq %rax, %rcx

call printf

movl 40(%rbp), %eax         ;eax as the accumulator

movl 32(%rbp), %edx         ;edx will hold the result

leal (%rdx,%rax), %eax

addl 48(%rbp), %eax

addl 56(%rbp), %eax

addl 64(%rbp), %eax

addl 72(%rbp), %eax

addl 80(%rbp), %eax

addl 88(%rbp), %eax

movl %eax, 32(%rbp)

movl 32(%rbp), %eax

movl %eax, %edx               ;pass through ecx, edx.

leaq .LC2(%rip), %rcx

call printf

leave

ret

.def printf; .scl 2; .type 32; .endef

 

 

What's New

このホームページを使って、

アセンブリ言語の紹介をします。