logo
down
shadow

assembly output of a simple C++ program


assembly output of a simple C++ program

By : Håvard Nordlie Mathi
Date : November 14 2020, 04:48 PM
Does that help I am trying to understand the assembly output of a simple c++ program. This is my C++ program. , Well, here it is line by line...
code :
 .file   "main.cpp"                # Debugging info (not essential)
    .text                          # Start of text section (i.e. your code)
    .globl  _Z4funcv               # Let the function _Z4funcv be callable
                                   # from outside (e.g. from your main routine)
    .type   _Z4funcv, @function    # Debugging info (possibly not essential)
_Z4funcv:                          # _Z4funcv is effectively the "name" of your 
                                   # function (C++ "mangles" the name; exactly 
                                   # how depends on your compiler -- Google "C++
                                   # name mangling" for more).
.LFB0:                             # Debugging info (possibly not essential)
    .cfi_startproc                 # Provides additional debug info (ditto)
     pushq  %rbp                   # Store base pointer of caller function
                                   # (standard function prologue -- Google 
                                   # "calling convention" or "cdecl")
    .cfi_def_cfa_offset 16         # Provides additional debug info (ditto)
    .cfi_offset 6, -16             # Provides additional debug info (ditto)
    movq    %rsp, %rbp             # Reset base pointer to a sensible place
                                   # for this function to put its local 
                                   # variables (if any).  Standard function
                                   # prologue.
    .cfi_def_cfa_register 6        # Debug ...
    popq    %rbp                   # Restore the caller's base pointer
                                   # Standard function epilogue
    .cfi_def_cfa 7, 8              # Debug...
    ret                            # Return from function
    .cfi_endproc                   # Debug...
.LFE0:                             # Debug...
    .size   _Z4funcv, .-_Z4funcv   # Debug...
    .globl  main                   # Declares that the main function
                                   # is callable from outside
    .type   main, @function        # Debug...
main:                              # Your main routine (name not mangled)
.LFB1:                             # Debug...
    .cfi_startproc                 # Debug...
    pushq   %rbp                   # Store caller's base pointer
                                   # (standard prologue)
    .cfi_def_cfa_offset 16         # Debug...
    .cfi_offset 6, -16             # Debug...
    movq    %rsp, %rbp             # Reset base pointer 
                                   # (standard prologue)
    .cfi_def_cfa_register 6        # Debug...
    call    _Z4funcv               # Call `func` (note name mangled)
    movl    $0, %eax               # Put `0` in eax (eax is return value)
    popq    %rbp                   # Restore caller's base pointer
                                   # (standard epilogue)
    .cfi_def_cfa 7, 8              # Debug...
    ret                            # Return from main function
    .cfi_endproc                   # Debug...
.LFE1:
    .size   main, .-main           # Debug...
    .ident  "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"  # fluff 
    .section    .note.GNU-stack,"",@progbits       # fluff
    .text                          
    .globl  _Z4funcv               
_Z4funcv:
    pushq  %rbp
    movq    %rsp, %rbp
    popq    %rbp
    ret
    .globl  main
main:
    pushq   %rbp
    movq    %rsp, %rbp
    call    _Z4funcv
    movl    $0, %eax
    popq    %rbp
    ret
   .text
    .globl  _func
_func:                   # Just as above, really
    push  %ebp
    mov    %esp, %ebp
    pop    %ebp
    ret
    .globl  _start
_start:                  # A few changes here
    push   %ebp
    mov    %esp, %ebp
    call    _func
    movl    $1, %eax     # Invoke the Linux 'exit' syscall
    movl    $0, %ebx     # With a return value of 0 (pick any char!)
    int $0x80            # Actual invocation


Share : facebook icon twitter icon
Assembly problem with simple program

Assembly problem with simple program


By : user2432940
Date : March 29 2020, 07:55 AM
this will help int 80 is a mechanism in some*a UNIX-like operating systems for making system calls.
For these calls, the registers are used for specific values. From the syscalls file:
code :
0 STD NOHIDE { int nosys(void); } syscall nosys_args int
1 STD NOHIDE { void exit(int rval); } exit rexit_args void
2 STD POSIX  { int fork(void); }
3 STD POSIX  { ssize_t read(int fd, void *buf, size_t nbyte); }
4 STD POSIX  { ssize_t write(int fd, const void *buf, size_t nbyte); }
move edx, len   ; length of message (nbyte).
move ecx, msg   ; message to print (buf).
move ebx, 1     ; file handle 1, stdout (fd).
move eax, 4     ; write syscall.
int  0x80       ; do it.

move ebx, 0     ; exit code (rval).
move eax, 1     ; exit syscall.
int 0x80        ; do it.
Tracing a simple program in assembly

Tracing a simple program in assembly


By : user3414377
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Well you are talking about two different things. If you want to see the contents of registers you need to execute the program so you need to make a binary. Targeted at a system, and then single step through it. yes gdb will work if you have the right gdb pointing at the right system. You can also use a jtag debugger, single step and then dump registers.
Little of this has anything to do with assembly language, you will want to see the instructions at an assembly language level when single stepping, sure, but you need to compile to a binary to run it.
code :
int run ( void )
{
    unsigned int ra;
    reset();
    while(1)
    {
        printf("-- 0x%08X --\n",reg_norm[15]-3);
        if(execute()) break;
        for(ra=0;ra< 8;ra++) printf("r%u 0x%08X ",ra,reg_norm[ra]); printf("\n");
        for(    ;ra<16;ra++) printf("r%u 0x%08X ",ra,reg_norm[ra]); printf("\n");

    }
    dump_counters();
    return(0);
}
int notmain ( void )
{
    int a = 4;
    int b = 5;
    int c;
    c = a + b;
    return(0);
}
00000074 <notmain>:
  74:   b580        push    {r7, lr}
  76:   b084        sub sp, #16
  78:   af00        add r7, sp, #0
  7a:   2304        movs    r3, #4
  7c:   60fb        str r3, [r7, #12]
  7e:   2305        movs    r3, #5
  80:   60bb        str r3, [r7, #8]
  82:   68fa        ldr r2, [r7, #12]
  84:   68bb        ldr r3, [r7, #8]
  86:   18d3        adds    r3, r2, r3
  88:   607b        str r3, [r7, #4]
  8a:   2300        movs    r3, #0
  8c:   1c18        adds    r0, r3, #0
  8e:   46bd        mov sp, r7
  90:   b004        add sp, #16
  92:   bd80        pop {r7, pc}
00000000 <hang-0x50>:
   0:   40080000    andmi   r0, r8, r0
   4:   00000053    andeq   r0, r0, r3, asr r0
   8:   00000051    andeq   r0, r0, r1, asr r0
...

00000052 <_start>:
  52:   f000 f80f   bl  74 <notmain>
  56:   df01        svc 1
  58:   e7fe        b.n 58 <_start+0x6>
00000074 <notmain>:
  74:   2000        movs    r0, #0
  76:   4770        bx  lr
int notmain ( void )
{
    return(0);
}
int xfun ( int a, int b )
{
    return(a+b);
}
int xfun ( int, int );
int notmain ( void )
{
    return(xfun(4,5));
}
00000080 <xfun>:
  80:   1808        adds    r0, r1, r0
  82:   4770        bx  lr
00000074 <notmain>:
  74:   b508        push    {r3, lr}
  76:   2004        movs    r0, #4
  78:   2105        movs    r1, #5
  7a:   f000 f801   bl  80 <xfun>
  7e:   bd08        pop {r3, pc}

00000080 <xfun>:
  80:   1808        adds    r0, r1, r0
  82:   4770        bx  lr
arm-linux-gcc -S -O2 notmain.c
mov r0, #4
mov r1, #5
b   xfun
arm-linux-gcc -S -O2 xbox.c
add r0, r1, r0
bx  lr
How to Write a simple Assembly Program

How to Write a simple Assembly Program


By : Radiatelabs
Date : March 29 2020, 07:55 AM
this will help "Howdy, Stranger" as they say in the movies...
As you realize, you need more code. First thing to learn is to exit cleanly - the CPU doesn't know when you're done. Depends on what OS. "ExitProcess", probably - usually it's Windoze when they don't say. :)
g++ Assembly Output of Simple Program using Virtual Inheritance

g++ Assembly Output of Simple Program using Virtual Inheritance


By : Saurabh Saxena
Date : March 29 2020, 07:55 AM
this one helps. I want to make sure I am understanding what my code is actually being compiled down to before an exe/library is made from it. I have the following program written in C++98. Which stems from this website http://www.phpcompiler.org/articles/virtualinheritance.html .
code :
_ZN4leftC2Ev:
.LFB6:
    .cfi_startproc                                        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;                      
    pushq   %rbp                                          ; LFB6 Associated with the adress for the class left constructor 
    .cfi_def_cfa_offset 16                                ;
    .cfi_offset 6, -16                                    ; %rdi, -8(%rbp) - his doesn't initialize anything - it is just pushing this value on local stack, in optimized version it will probably dissapear. 
    movq    %rsp, %rbp                                    ; %rsi, -16(%rbp) - just as above;
    .cfi_def_cfa_register 6                               ; %rsi - pointer to virtual table table (not a mistake it's vtt, not vt) for left-in-bottom
    movq    %rdi, -8(%rbp)                                ;  %rdi - pointer to left instance
    movq    %rsi, -16(%rbp)                               ;
    movq    -16(%rbp), %rax                               ; What does the rest of this do?
    movq    (%rax), %rdx                                  ;  It is just copying vtable address form vtt to first eight bytes of actual object.
    movq    -8(%rbp), %rax
    movq    %rdx, (%rax)
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
    movl    $_ZTV6bottom+24, %edx
    movq    -8(%rbp), %rax
    movq    %rdx, (%rax)
    movl    $_ZTV6bottom+48, %edx
    movq    -8(%rbp), %rax
    movq    %rdx, 16(%rax)
main:
.LFB0:
    .cfi_startproc                                        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    pushq   %rbp                                          ; Store off the base pointer
    .cfi_def_cfa_offset 16                                ; Debug code to trace where stack pointer is?
    .cfi_offset 6, -16                                    ;
    movq    %rsp, %rbp                                    ; Move where stack pointer is to base pointer
    .cfi_def_cfa_register 6                               ; 
    pushq   %rbx                                          ; Store off what might have been in rbx
    subq    $24, %rsp                                     ; Push argc onto stack
    .cfi_offset 3, -24                                    ;
    movl    $40, %edi                                     ; Push argv onto stack
    call    _Znwm                                         ; Call new
    movq    %rax, %rbx                                    ; Create room for b %rax contains address of memory
    movq    $0, (%rbx)                                    ; location where new returned?
    movq    $0, 8(%rbx)                                   ; A: Yes - eax contains address of returned buffer
    movq    $0, 16(%rbx)                                  ;    It is being zeroed to 5*8 = 48 bytes
    movq    $0, 24(%rbx)                                  ;
    movq    $0, 32(%rbx)                                  ; 
    movq    %rbx, %rdi                                    ; Move that data into dynamic memory? -hmmm, what? Just moving pointr to it into rdi, where bottom constructor expects it
                                                          ; it is still where it was - in dynamic memory from new (_Znwm), jut it's pointer changed register ;)
    call    _ZN6bottomC1Ev                                ; Call the construtor of the object
    movq    %rbx, -32(%rbp)                               ; 
    movq    -32(%rbp), %rax                               ; Here isn't happening much - classes bottom and left are sure to start at the same address, so compiler doesn't need to chack for anything,
    movl    $5, 8(%rax)                                   ; just use offset to addres fields, and copy the pointer without modification to do the castting from bottom to left.
    movq    -32(%rbp), %rax                               ; 
    movq    %rax, -24(%rbp)
    movq    -24(%rbp), %rax
    movl    8(%rax), %eax
    movl    %eax, %esi
    movl    $.LC0, %edi
    movl    $0, %eax
    call    printf
    movl    $0, %eax
    addq    $24, %rsp
    popq    %rbx
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
A simple assembly program?

A simple assembly program?


By : user3780317
Date : March 29 2020, 07:55 AM
this one helps. Here is the MASM documentation online: http://web.sau.edu/LillisKevinM/csci240/masmdocs/
Here is the difinitive tutorial on assembly: http://webster.cs.ucr.edu/
Related Posts Related Posts :
  • How to wrap std::chrono in a c++ iterator type
  • Check if a socket is connected in mac os x
  • Grabbing the mouse cursor in GLFW
  • Calling Windows shell menu (same as right-click in Explorer) for multiple files programmatically
  • Pop up dialog to save file in seperate thread
  • Why would you use the keyword const if you already know variable should be constant?
  • Detecting Small Sound Effects In C++
  • How to avoid the copy when I return
  • Getting error code C2228: left of '._Ptr' must have class/struct/union
  • In win32 C++ programming how can I duplicate a window style?
  • Should the order of import statements matter when importing a .so?
  • Design decision regarding std::array fill
  • Access array of C++ structs from Fortran?
  • Determining when functions allocate memory in C++
  • C++ using std::set remove duplicate item for a struct fail.
  • Code to find out the number of triplets which lead to a sum less than or equal to threshold is giving a wrong output
  • Converting glm::lookat matrix to quaternion and back
  • Searching multi-dimensional vectors
  • Send and receive via SOCKS5 c++
  • GDI+: unhandled exception when drawing bitmap
  • Editing other processes memory
  • Object deleted when its member function is being executed?
  • Infinite Loop while inputting the different data type values in STACK
  • const members and operator=
  • Threaded Video Player sync
  • Does infinite of floating point number satisfy these equation?
  • map sorting in c++ by the frequencies
  • The Preprocessor program linked in g++ similar to the cpp program in gcc
  • How is the union used in this OpenCV library?
  • C++ memory leak in recursion
  • C++ Error: C4430 and C2143 Error at an impossible place
  • How can I track object lifetime in C++11 lambda?
  • #include statement mapping in Biicode (biicode.conf)
  • std::equal gives "Term doesnt evaluate to a function taking 2 arguments"
  • C++ template argument as reference lvalue
  • Legal to forward declare C standard library entities but not C++ standard library entities?
  • Conversion of wchar_t* to string
  • VirtualTreeView - Embarcadero C++ Builder XE7 - 64 bits
  • I don't understand C++ pointer arithmetic
  • Invalid addition of constness? Error: Cannot use char** to initialize const char**
  • Initilize constructor of class from another class
  • what happens when a class object is used as an index for an array?
  • Read and straighten multiple images from vector string, get error: "vector subscript out of range" [c++]
  • Meaning of a few lines in C++
  • Map, pair-vector or two vectors...?
  • Redefinition error in ostream overload in template and inherited classes
  • CUDA 6.5: error MSB3191 Unable to create directory and LNK2001 Unresolved External symbol
  • Is it possible to overlap batched FFTs with CUDA's cuFFT library and cufftPlanMany?
  • How to delete function from DLL binary
  • How do I loop over a boost MPL list of non-default constructed classes?
  • Download page using IE engine + use POST
  • How to insert an element into ublas matrix with dynamic size
  • Using typedefs appropriately to avoid "typedef contamination"
  • C++ Simple Converting from Binary to Decimal
  • error deleting item from list after passing through function
  • C++: RVO, NRVO and returning local objects
  • performance map c++ find (g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3))
  • How to change the fundamental underlying container type for the adaptor containers?
  • Crash when SAFE_RELEASE is called on IMMDeviceEnumerator
  • Visual Studio C++, how to change the text color for "nullptr"
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org