Examining the Assembly Listing

This is a discussion on Examining the Assembly Listing within the C Programming forums, part of the General Programming Boards category; This is my third project in this class involving having to identify what machine instruction is doing what, and I ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    29

    Question Examining the Assembly Listing

    This is my third project in this class involving having to identify what machine instruction is doing what, and I just want to see if I'm doing it right or wrong because that would suck to get three F's! I'm not trying to cheat, so try giving me advice without just giving me the answer. Thanks!

    The following is the assembly code generated from a short program I wrote to give the sum of an int and a char:

    Code:
    ; Listing generated by Microsoft (R) Optimizing Compiler Version 15.00.21022.08 
    
    	TITLE	c:\Users\C\Documents\Visual Studio 2008\Projects\proj6vcsolut\proj6vc\proj6.cpp
    	.686P
    	.XMM
    	include listing.inc
    	.model	flat
    
    INCLUDELIB MSVCRTD
    INCLUDELIB OLDNAMES
    
    PUBLIC	?z@@3HA						; z
    PUBLIC	?x@@3HA						; x
    PUBLIC	?y@@3DA						; y
    _BSS	SEGMENT
    ?z@@3HA	DD	01H DUP (?)				; z
    ?x@@3HA	DD	01H DUP (?)				; x
    ?y@@3DA	DB	01H DUP (?)				; y
    _BSS	ENDS
    PUBLIC	?sumfunc@@YAHHD@Z				; sumfunc
    EXTRN	__RTC_Shutdown:PROC
    EXTRN	__RTC_InitBase:PROC
    ;	COMDAT rtc$TMZ
    ; File c:\users\c\documents\visual studio 2008\projects\proj6vcsolut\proj6vc\proj6.cpp
    rtc$TMZ	SEGMENT
    __RTC_Shutdown.rtc$TMZ DD FLAT:__RTC_Shutdown
    rtc$TMZ	ENDS
    ;	COMDAT rtc$IMZ
    rtc$IMZ	SEGMENT
    __RTC_InitBase.rtc$IMZ DD FLAT:__RTC_InitBase
    ; Function compile flags: /Odtp /RTCsu /ZI
    rtc$IMZ	ENDS
    ;	COMDAT ?sumfunc@@YAHHD@Z
    _TEXT	SEGMENT
    _z$ = -8						; size = 4
    _x$ = 8							; size = 4
    _y$ = 12						; size = 1
    ?sumfunc@@YAHHD@Z PROC					; sumfunc, COMDAT
    
    ; 9    : int sumfunc(int x, char y){
    
    	push	ebp
    	mov	ebp, esp
    	sub	esp, 204				; 000000ccH
    	push	ebx
    	push	esi
    	push	edi
    	lea	edi, DWORD PTR [ebp-204]
    	mov	ecx, 51					; 00000033H
    	mov	eax, -858993460				; ccccccccH
    	rep stosd
    
    ; 10   :      int z;
    ; 11   :      z= x + y;
    
    	movsx	eax, BYTE PTR _y$[ebp]
    	add	eax, DWORD PTR _x$[ebp]
    	mov	DWORD PTR _z$[ebp], eax
    
    ; 12   :      return z;
    
    	mov	eax, DWORD PTR _z$[ebp]
    
    ; 13   :      }
    
    	pop	edi
    	pop	esi
    	pop	ebx
    	mov	esp, ebp
    	pop	ebp
    	ret	0
    ?sumfunc@@YAHHD@Z ENDP					; sumfunc
    _TEXT	ENDS
    PUBLIC	??_C@_05PDJBBECF@pause?$AA@			; `string'
    PUBLIC	??_C@_0CC@CNFEEKMM@The?5sum?5of?5the?5two?5numbers?5is?5?$CFi@ ; `string'
    PUBLIC	??_C@_02DPKJAMEF@?$CFd?$AA@			; `string'
    PUBLIC	??_C@_0BO@PFLLPLLJ@Please?5type?5another?5integer?4?6?$AA@ ; `string'
    PUBLIC	??_C@_02IKAHHCAI@?$CFi?$AA@			; `string'
    PUBLIC	??_C@_0BJ@BLCGGBIN@Please?5type?5an?5integer?4?6?$AA@ ; `string'
    PUBLIC	_main
    EXTRN	__imp__system:PROC
    EXTRN	__imp__scanf:PROC
    EXTRN	__imp__printf:PROC
    EXTRN	__RTC_CheckEsp:PROC
    ;	COMDAT ??_C@_05PDJBBECF@pause?$AA@
    CONST	SEGMENT
    ??_C@_05PDJBBECF@pause?$AA@ DB 'pause', 00H		; `string'
    CONST	ENDS
    ;	COMDAT ??_C@_0CC@CNFEEKMM@The?5sum?5of?5the?5two?5numbers?5is?5?$CFi@
    CONST	SEGMENT
    ??_C@_0CC@CNFEEKMM@The?5sum?5of?5the?5two?5numbers?5is?5?$CFi@ DB 'The su'
    	DB	'm of the two numbers is %i', 0aH, 00H	; `string'
    CONST	ENDS
    ;	COMDAT ??_C@_02DPKJAMEF@?$CFd?$AA@
    CONST	SEGMENT
    ??_C@_02DPKJAMEF@?$CFd?$AA@ DB '%d', 00H		; `string'
    CONST	ENDS
    ;	COMDAT ??_C@_0BO@PFLLPLLJ@Please?5type?5another?5integer?4?6?$AA@
    CONST	SEGMENT
    ??_C@_0BO@PFLLPLLJ@Please?5type?5another?5integer?4?6?$AA@ DB 'Please typ'
    	DB	'e another integer.', 0aH, 00H		; `string'
    CONST	ENDS
    ;	COMDAT ??_C@_02IKAHHCAI@?$CFi?$AA@
    CONST	SEGMENT
    ??_C@_02IKAHHCAI@?$CFi?$AA@ DB '%i', 00H		; `string'
    CONST	ENDS
    ;	COMDAT ??_C@_0BJ@BLCGGBIN@Please?5type?5an?5integer?4?6?$AA@
    CONST	SEGMENT
    ??_C@_0BJ@BLCGGBIN@Please?5type?5an?5integer?4?6?$AA@ DB 'Please type an '
    	DB	'integer.', 0aH, 00H				; `string'
    ; Function compile flags: /Odtp /RTCsu /ZI
    CONST	ENDS
    ;	COMDAT _main
    _TEXT	SEGMENT
    _main	PROC						; COMDAT
    
    ; 15   : int main(void){
    
    	push	ebp
    	mov	ebp, esp
    	sub	esp, 192				; 000000c0H
    	push	ebx
    	push	esi
    	push	edi
    	lea	edi, DWORD PTR [ebp-192]
    	mov	ecx, 48					; 00000030H
    	mov	eax, -858993460				; ccccccccH
    	rep stosd
    
    ; 16   :    
    ; 17   :     printf("Please type an integer.\n");
    
    	mov	esi, esp
    	push	OFFSET ??_C@_0BJ@BLCGGBIN@Please?5type?5an?5integer?4?6?$AA@
    	call	DWORD PTR __imp__printf
    	add	esp, 4
    	cmp	esi, esp
    	call	__RTC_CheckEsp
    
    ; 18   :     scanf("%i", &x);
    
    	mov	esi, esp
    	push	OFFSET ?x@@3HA				; x
    	push	OFFSET ??_C@_02IKAHHCAI@?$CFi?$AA@
    	call	DWORD PTR __imp__scanf
    	add	esp, 8
    	cmp	esi, esp
    	call	__RTC_CheckEsp
    
    ; 19   :     printf("Please type another integer.\n");
    
    	mov	esi, esp
    	push	OFFSET ??_C@_0BO@PFLLPLLJ@Please?5type?5another?5integer?4?6?$AA@
    	call	DWORD PTR __imp__printf
    	add	esp, 4
    	cmp	esi, esp
    	call	__RTC_CheckEsp
    
    ; 20   :     scanf("%d", &y);
    
    	mov	esi, esp
    	push	OFFSET ?y@@3DA				; y
    	push	OFFSET ??_C@_02DPKJAMEF@?$CFd?$AA@
    	call	DWORD PTR __imp__scanf
    	add	esp, 8
    	cmp	esi, esp
    	call	__RTC_CheckEsp
    
    ; 21   :     printf("The sum of the two numbers is %i\n", sumfunc(x,y));
    
    	movzx	eax, BYTE PTR ?y@@3DA			; y
    	push	eax
    	mov	ecx, DWORD PTR ?x@@3HA			; x
    	push	ecx
    	call	?sumfunc@@YAHHD@Z			; sumfunc
    	add	esp, 8
    	mov	esi, esp
    	push	eax
    	push	OFFSET ??_C@_0CC@CNFEEKMM@The?5sum?5of?5the?5two?5numbers?5is?5?$CFi@
    	call	DWORD PTR __imp__printf
    	add	esp, 8
    	cmp	esi, esp
    	call	__RTC_CheckEsp
    
    ; 22   :     
    ; 23   :     
    ; 24   :     system("pause");    
    
    	mov	esi, esp
    	push	OFFSET ??_C@_05PDJBBECF@pause?$AA@
    	call	DWORD PTR __imp__system
    	add	esp, 4
    	cmp	esi, esp
    	call	__RTC_CheckEsp
    
    ; 25   :     return 0;
    
    	xor	eax, eax
    
    ; 26   : }
    
    	pop	edi
    	pop	esi
    	pop	ebx
    	add	esp, 192				; 000000c0H
    	cmp	ebp, esp
    	call	__RTC_CheckEsp
    	mov	esp, ebp
    	pop	ebp
    	ret	0
    _main	ENDP
    _TEXT	ENDS
    END

    And I answered the questions required as follows:

    a. What machine instruction is used to actually invoke (run, transfer control to) your function?
    "call"

    b. What code got generated to return the result of the function to main?
    "mov"

    c. What machine instruction is used to assign a value to a variable? (corresponding to the =
    assignment operator)?
    "mov"

    d. What assembly language instruction is used to pass parameter values to your function?
    (Hint: it involves the concept of the stack).
    "movzx"

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    I think you need to be more specific than just stating single instructions.

    What grade does 3 out of 4 get you?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    More like this? 3/4 is better than I anticipated but would be nice to get them all right, lol.

    a. What machine instruction is used to actually invoke (run, transfer control to) your function?
    "call ?sumfunc@@YAHHD@Z"

    b. What code got generated to return the result of the function to main?
    "mov eax, DWORD PTR _z$[ebp]"

    c. What machine instruction is used to assign a value to a variable? (corresponding to the = assignment operator)?
    "mov DWORD PTR _z$[ebp], eax"

    d. What assembly language instruction is used to pass parameter values to your function? (Hint: it involves the concept of the stack).
    "movzx eax, BYTE PTR ?y@@3DA
    push eax
    mov ecx, DWORD PTR ?x@@3HA"

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Quote Originally Posted by quintenmater View Post
    b. What code got generated to return the result of the function to main?
    "mov eax, DWORD PTR _z$[ebp]"
    How exactly does a "mov" instruction alter the control flow of a running program?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by Sebastiani View Post
    How exactly does a "mov" instruction alter the control flow of a running program?
    Read carefully:

    What code got generated to return the result of the function to main?
    This is distinct from returning control to the calling function.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    meh- I don't know its the only instruction after "return z;" Am I looking in the wrong place? Since the result would be in the z=x+y then should I be looking at the instructions under it: movsx eax, BYTE PTR _y$[ebp]
    add eax, DWORD PTR _x$[ebp]
    mov DWORD PTR _z$[ebp], eax
    ??

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    For "b. What code got generated to return the result of the function to main?"

    Am I getting warmer or colder by stating "The value is stored by a function in the eax register which is taken to be the return value of the function" ??

    I found that answer here:
    From C To Assembly Language LG #94

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    It looks like you've got it sorted now.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    1
    Quote Originally Posted by quintenmater View Post
    For "b. What code got generated to return the result of the function to main?"

    Am I getting warmer or colder by stating "The value is stored by a function in the eax register which is taken to be the return value of the function" ??

    I found that answer here:
    From C To Assembly Language LG #94

    As for part b:
    Yes, what you said is right but because he's asking "what code got generated?" I'd also provide the actual machine code. And for you that's going to be the following:
    mov eax, DWORD PTR _z$[ebp]

    And because your function call is in a printf statement, you don't really see (or at least I cannot tell) when or where the actual value of z (which got moved to eax, in the code above) is being dealt with. Maybe its because its happening within the printf call and we can't see it?

    Anyway, good luck!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Assembly
    By mrafcho001 in forum Tech Board
    Replies: 5
    Last Post: 03-12-2006, 04:00 PM
  2. Disassembler
    By siavoshkc in forum C++ Programming
    Replies: 17
    Last Post: 03-01-2006, 04:43 AM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 02:35 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21