# Calling Conventions

In computer science, a **calling convention** is an implementation-level (low-level) scheme for how subroutines receive parameters from their caller and how they return a result. Differences in various implementations include where parameters, return values, return addresses and scope links are placed (registers, stack or memory etc.), and how the tasks of preparing for a function call and restoring the environment afterwards are divided between the caller and the callee.

The Well-known calling conventions are:

* \_\_cdecl
* \_\_stdcall
* \_\_fastcall

In the first two calling conventions, the parameters of the function are pushed onto the stack in reverse order. For Example: if there is a function "void myFunction(int a,int b)".The function will be assembled as:-

```asmatmel
push b
push a
call myFunction
```

![](https://1846927083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mj9GKP_mf1AnaApJEIA%2Fuploads%2F5iY25lvHKeZ0BoxnRdXo%2Fimage.png?alt=media\&token=0133b9a5-2c0c-4850-b5bb-264da0781335)

![](https://1846927083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mj9GKP_mf1AnaApJEIA%2Fuploads%2FQXioMu1Tq202dbb3Vvn7%2Fimage.png?alt=media\&token=036d3c28-ca63-4567-9271-5f40de1adc70)

![](https://1846927083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mj9GKP_mf1AnaApJEIA%2Fuploads%2FKCG5cPTcLteXISYRVq1S%2Fimage.png?alt=media\&token=8c57969a-adde-4520-88b5-29441cdbe745)

![](https://1846927083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mj9GKP_mf1AnaApJEIA%2Fuploads%2FnBTIRFMZE9ovvwTJeBGp%2Fimage.png?alt=media\&token=79032871-59d5-4d3f-b8ff-2b661e62d189)

![](https://1846927083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mj9GKP_mf1AnaApJEIA%2Fuploads%2FzCKGjlgIx3jS8UZEkVxb%2Fimage.png?alt=media\&token=fddb0a9f-766e-4ebb-b9d0-0798ca9ce986)

![](https://1846927083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mj9GKP_mf1AnaApJEIA%2Fuploads%2FEUp0XWskaNPDPj9d3H5s%2Fimage.png?alt=media\&token=0934b27f-3c80-4761-9928-aca50847e34e)

```asmatmel
push a
push b
call MyFunction
test eax, eax
je _somewhere
```

![](https://1846927083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mj9GKP_mf1AnaApJEIA%2Fuploads%2FginJWBXqrUXi3MOMnVPe%2Fimage.png?alt=media\&token=7e400b35-3e6a-4f7b-aa17-975659d4b2a6)

```asmatmel
mov esp,ebp 
pop ebp
ret 8 <- cleanup the stack from paramenters a and b
```

## \_\_cdecl

```asmatmel
push a
push b
Call MyFunction
add esp, 8
test eax, eax
```

![](https://1846927083-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mj9GKP_mf1AnaApJEIA%2Fuploads%2FkKErieAjRrb3sjh0qFfO%2Fimage.png?alt=media\&token=0b77906d-2ce9-4448-aff9-840ebfacbc48)
