Loading...

x64 Assembly and C++ Tutorial 9: Passing Integers to and from C++ and Assembly

9,940 views

Loading...

Loading...

Transcript

The interactive transcript could not be loaded.

Loading...

Loading...

Rating is available when the video has been rented.
This feature is not available right now. Please try again later.
Published on Sep 25, 2011

This is the 9th tutorial, we'll be looking at passing integer values to and from C++ and Assembly. The calling convention here is dictated by the C++ compiler we're using. In Microsoft's C++ compiler the parameters are passed by what's known as 'FastCall'. FastCall is not a calling convention but a generic term for several different methods of passing parameters via the registers. The FastCall we will be examining is the Microsoft version, Linux uses 'System V fastcall' which allows for a few more parameters to be passed prior to resorting to the stack.

Integer values are always returned in RAX or the appropriate denomination of RAX (EAX, AX or AL). We pass the first four parameters via the appropriate denomination of RCX, RDX, R8 and then R9. So the first integer would be passed in ECX. The first short integer would be passed in CX. Any parameters subsequent to the first four are passed via the stack. We will look at the stack in a future tutorial.

Passing floating point numbers will have to wait until we look at SSE. Pointers are just QWords and are passed in an indentical manner to the first four integer parameters. We will look at RAM and pointers shortly.

I want also to point out that calling conventions are just that, conventions. We need to know how the Microsoft C++ compiler will pass parameters to us but once we're in Assembly we can pass parameters however we like. It's always a good idea to stick to some predefined, tried and true calling convention but in Assembly speed is often paramount. If you can gain clock ticks with an obscure sleight of hand then you're welcome to throw the calling conventions out the window. Document what you're doing and why so that later you or other programmers aren't confused, and then you're free to push the pedal to metal!

Both the beauty and danger of Assembly is this lack of rules. You can easily speed a program up but it's even easier to crash it. Be careful and comment everything!

  • Category

  • License

    • Standard YouTube License

Loading...

Advertisement
When autoplay is enabled, a suggested video will automatically play next.

Up next


to add this to Watch Later

Add to

Loading playlists...