Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Newbie Assembly Question

Name: Anonymous 2009-09-03 12:00

Stupid question:

I call the following function:

asm("movl %esp, %eax");

in part of my program and I get the stack pointer (no problems so far).

Why does the asm function return whatever is in the EAX register? Is that the point of the function, and if so why (i.e. why not let it return a value of your choosing, etc.)? This might seem incredibly stupid, but I have been learning Motorola 68000 processor architecture while looking at fiddling with assembly through high-level languages and I don't have a great grasp on x86 architecture on top of that...

Thanks in advance

Name: Anonymous 2009-09-03 12:50

I'm pretty sure the return value of the asm pseudofunction is undefined.

Name: Anonymous 2009-09-03 13:28

Maybe the full function will help:

#include <stdlib.h>

unsigned long sp(void){ asm("movl %esp, %eax");}
int main(int argc, char *argv[])
{
long esp;
esp = sp();
printf("Stack Pointer (ESP) : 0x%x\n", esp);
return 0;
}

So all the program does is print whatever sp() gives, which apparently is the sp. But I don't get why asm returns a value.

Name: Anonymous 2009-09-03 13:33

>>3
Please use the [code] tags. Thank you.

Name: Anonymous 2009-09-03 13:52

>>2
8.2.1.5 Returning a Value from the Procedure

Assembly can return values to the C calling program using only the EAX register. If the returned value is only four bytes or less, the result is returned in register EAX. If the item is larger than four bytes, a pointer is returned in EAX which points to the item. Here is a short table of the C variable types and how they are returned by the assembly code:

http://courses.ece.illinois.edu/ece390/books/labmanual/c-prog-mixing.html
I believe there is a way to get the contents of other registers to be returned using extended asm, but I'm not sure at the moment

Name: Anonymous 2009-09-03 13:57

Read up on calling conventions. Or read >>5. Oh, and if you're going to use GCC, you might as well go for extended ASM syntax, which is pretty awesome once you get into it (which will take a while, given how poorly documented this shit is).

Name: Anonymous 2009-09-03 14:05

>>5
Thanks for that.

Name: Anonymous 2011-01-18 6:12

test

test
test


test
> test


test> test

test
test

Name: Anonymous 2011-02-03 6:06

Name: Anonymous 2011-02-04 14:24

Name: Anonymous 2013-08-31 22:41



   /i
  .| |
  | |
 .| |
 | |    __ノ ̄i. __,,,.........,,,__
 | |    ) ヽ、_ゝ-        `ヽ、
 | |    `y'"        `ヽ、 ヽ,
 .| |.    イ / / ⌒/ i  i⌒', ',   ',
 .| |    Lハ__L/ i__ハ__ハ___i .l   i
  | |.     ノ iT!'j   <  ト-┘  |
  .i i     ´7〈""  r─,  ""|   ノ  ..:::::::...
  l l__   く__,iゝ、_ ヽノ  ,.イ_ノ_i^i  ::   ::::::
 ⊂ ̄_ゝ、    `´_'ヽ'ハヘT´_/ヽ-ハ,.ト-、 ::  ::::::
  `! (、 i、  ,γ´ ヘ (ハ)´〒 /(つ   〉:  :::::
   iミiヽ、   ̄)  /  Y 。  i _`T ´|  :::::
   .lミl  ` ̄ ^ーイ  ハ ゚  .ハ"7/  i  :::
   

Name: Anonymous 2013-09-01 0:13



          ,.へ,.へ -‐─- 、.,_
         ,. >、 `く       `' 、
       ., ' く_,.へ._> ,       ヽ、
       /    . '´  / l    ハ 、 ハ`フ   ___
      ,'   ,'   ,' /__'、   |/_|_ ハ  'r'     | r──┘ 
      l   / ,  .|/__」/_、 | _」/! }  }     | | l二二`l
      | ∠.イ|  「 l)l::}` \|´l:l!}|/ 八    | | く\/ /
      ',   八   'ひ-'    `"oイ ̄`   ; ;  > ,.く
       ',  /  l 、._,ゞ"      "}リ      く/ く/\>
       }     |  ト 、   ´   ,.イ!
       ノ    八 ', }  ーrァ升| |        r、 「l r、
   ト-‐''´    ,:' ,ハ ∨\ _ハ ヽ! |       くノ | /ゝ'
   `>'´   //´ヽレ'、|`}_アrr'7ハ |        / ' ー┐
  /    , '  '   ∨   ○ ∨!八      r'_イ.l三 l l
 ,: '     /    {    、   △  } |   ',       | l二 l |  
 {     ;    ∧    ヽ  × | |   ,.       └' ̄`┘
  、    、   ;  ',     \.八 '、!   }
  \    \ , く \   /`7ヽYヽ  ;     ___「l___
    )  ノ, /  `ア`' 、/ /__八,ハ}、/     | r‐┐r‐┐!
   (r'´ (, '     ./    ` ´ ヽ  ̄ ヽ.      | |_| |_| |
    `   {     ;         ',    ハ.     └‐ ┐r─┘
      ,-{ ` 、.,_、        ,     }        .|_|
    / ,r>、.,__ 、>-‐- 、.ノ  ノ    rノ、
     {_く_r、_,,.. -、_,.. -、_>イ__.>-く__r'

Name: Anonymous 2013-09-01 1:44



         __,,.. -‐─- 、.,_
        _r'`i ̄Y ̄7ー、_. `ヽ、.,
      r'´i>'‐- 、─-ァ' i‐-、 、ヽ、,__
      Y´        ̄`ヽ/-、 ヽr--ヽ.
     ノ  i /i ハ  ハ 、   Y´i  `''ー- 、  十六夜の異名を持ち
    ∠_/ /ト,、レ' |/_,.!イi i  iン   く⌒ヽ
     ノへ .!ハ.リ  'ァ;‐-'、 ハ ,ゝ    )    時間を自在に操る
     .ンrン!"´.     !_ン^r'Y    ,へ(ヽ.
   く7_,.r'ンヽ.,   _    "/)'ン、/iへ〈   !ノ    瀟洒なるメード長
  /ンムー'  .レiヽ、__,.. イン'7`/::::::::ヽ._  /
  '´   ./i __  レヘrン´ン´ ̄`ヽ;:::::::::Y´
     ///  }>'く{/      i:::::::::::i
    r'-'_ァ     レ7ed      |:::::::::::|,,.. -‐ァ
  r'´ ̄Yi  _____,/ヽ,-、 Ma /::::::::::::'r- 〈
  ヽ.  ´ ̄     ,,. '"、>r<_:::::::::::::::::ハ_,. ->
    `ー-‐─---‐'"  _,.'-ァ〉`''ァー--‐r'ヽ!<.,__,,.. -‐ァ
            r/:::::::/i  /   /:::::ハヽ、.,___ く
          r'"´::::::::::::Y    /:::::/::::::',___   ̄`
         く7´ ̄ヽ、__くj   ,:'::::::::::::::::::::ヽ、`i
         /  ̄`ヽ./ヽ!   /:::::::/:::::::::i::::::::::7、
       ,./    , 'ヽ、:;_ン、_」-'"`ヽ、_:::::|::::/r'
     , く.   _,. '´     /   ̄`ヽ∠>、/ン
    ,く.,,__ >'"       /    /ヽ.  
   /ヘ、__/         !   イ>ヘ:::l |
   i::::lコ::|         `'ー-‐'"i;:--、l |
   !:::::::::::!             ヽ二ン
   ヽ二ン

Name: Anonymous 2013-09-01 3:14



          ト、   __,,,,.....,,,__
          / !'"´ ___  `゙' 、
        ./   , ''"´ ̄ ̄/`ヽ=-、 `'ー-‐ァ
         /   /            (`'´)k'
       ;  ./   /   ./    } 、 `'´八>
      ./{/l   / -/-/ |   ハ  ;     \
      /|  |    |r;テ=r-、 !  ,:' }- |  } r‐-ヽ
    , '  ; ノ   | ゝ-┴  |/ rテr !  ,' ;
    /  /∠ ヘ.   '、xx       ヾl!// /
   .{ ∠、 i  ト、.,_,,.>       xイl|/`ヽ
   '、  ) 、  '、  `ヽ、  ´    人ハ´   }
     \(/ \ } \  } >‐ァ<´|  ノ   /
      `ヽ.  ,> ' ∨、  / ト 、 ノ-イ   /
       r<ヽヽ    \ ン'  \ ト‐'´ 
       ./´  \\ヽ   ム   }r'ヽ
      _,!    ∨}ヽr-‐ァ(`'´)くl   } ̄`ヽ     , -,
  , '"´  /|.     ∨ ̄   `Y  _ハ ム   ノ r 、 .,:' ./
  .{   / .;      }      },.'´_`ヽ}-‐''  } } / ./_
  '、  r'       ;、     ({く弋ノ .>}\  _{  ヽ/´/ヽ
    `7        ム゙      ゝ、,__,.ノl  `「 ゝ、  l_/_/ノ

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List