Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. asm-101
  3. function-call
3 min read

Function Call

A program is rarely one straight run of instructions — it is built from functions that call each other. This challenge is about doing that correctly: splitting code into a function and calling it the way the rest of the system expects, following the System V convention from Stack & Calls.

Problem

Define a function that takes one integer argument and returns a value, then call it from _start, passing a single argument. Honor the calling convention — argument in rdi, return value in rax — and use the returned value as the program's exit status so you can verify it from the shell.

Hints

Open one hint at a time before reading the walk-through.

Hint 1
The first integer argument travels in rdi, and the return value comes back in rax. That is the whole contract between caller and callee (Stack & Calls).
Hint 2
call pushes the return address and jumps to the function; ret pops it back so execution resumes after the call. Put the function body after _start exits, so control never falls into it by accident.
Hint 3
To see the result, copy rax into rdi and call exit — then read it back with echo $?.

Walk-through

Let the function be times_two(n), returning n * 2. In _start, load the argument into rdi (here 21) and call times_two. By the convention the function will leave its answer in rax, so move rax into rdi and exit with it.

The function itself is three instructions: copy the incoming argument out of rdi into rax, double it with add rax, rax, and ret. Because _start exits with its own syscall before the function's address, control only ever reaches times_two through the call. Watch the argument arrive and the result come back:

gdb
gef➤ break times_twoBreakpoint 1 at 0x401014gef➤ runBreakpoint 1, 0x0000000000401014 in times_two ()gef➤ p/d $rdi$1 = 21gef➤ break *0x40101aBreakpoint 2 at 0x40101agef➤ continueBreakpoint 2, 0x000000000040101a in times_two ()gef➤ p/d $rax$2 = 42

At the function's entry rdi holds 21 — the argument, exactly where the convention says it will be. By the ret, rax holds 42, the doubled result ready for the caller to use.

Solution

nasm
1section .text
2    global _start
3_start:
4    mov rdi, 21         ; first argument in rdi
5    call times_two      
shell
$ ./fn; echo $?42

42 is 21 * 2 — the argument went in through rdi, the function returned its answer in rax, and the caller used it. That round trip is every function call on the platform, in miniature.

; result returned in rax
6 mov rdi, rax ; exit code = return value
7 mov rax, 60
8 syscall
9
10times_two:
11 mov rax, rdi ; copy the argument
12 add rax, rax ; double it
13 ret ; return value is in rax