Capstone: command-line calculator
The goal
Build a reverse-Polish-notation calculator: calc 3 4 + prints 7. It walks argv, pushes numbers on a stack, applies operators, and prints the final value. It uses argv parsing, a manual stack, integer division, and printing a number in decimal.
Design
The stack is an array of qwords in .bss with a top pointer in a register. Parsing argv: each argument is either an operator (+ - * /) or a number to convert with the classic multiply-by-ten loop. Printing converts the result back by repeated division by 10, storing remainders, then printing the digits in reverse:
default rel %define SYS_WRITE 1 %define SYS_EXIT 60 section .bss stack resq 64 outbuf resb 32 section .text global _start _start: pop r12 ; argc mov rbx, rsp ; argv xor r15, r15 ; stack top dec r12 ; arguments after the program name add rbx, 8 ; skip argv[0] arg_loop: test r12, r12 jz finish mov rsi, [rbx] mov al, [rsi] cmp byte [rsi + 1], 0 jne number cmp al, "+" je op_add cmp al, "-" je op_sub cmp al, "*" je op_mul cmp al, "/" je op_div number: xor rax, rax xor rcx, rcx parse: movzx rdx, byte [rsi + rcx] test rdx, rdx jz push_value sub rdx, "0" imul rax, rax, 10 add rax, rdx inc rcx jmp parse op_add: call pop_two add rax, rdx jmp push_value op_sub: call pop_two sub rax, rdx jmp push_value op_mul: call pop_two imul rax, rdx jmp push_value op_div: call pop_two xor rdx, rdx div rcx jmp push_value push_value: mov [stack + r15 * 8], rax inc r15 add rbx, 8 dec r12 jmp arg_loop finish: dec r15 mov rax, [stack + r15 * 8] lea rdi, [outbuf + 31] ; build the digits backwards mov byte [rdi], 10 dec rdi mov rcx, 10 digits: xor rdx, rdx div rcx add dl, "0" mov [rdi], dl dec rdi test rax, rax jnz digits inc rdi lea rdx, [outbuf + 32] sub rdx, rdi mov rsi, rdi mov rax, SYS_WRITE mov rdi, 1 syscall mov rax, SYS_EXIT xor rdi, rdi syscall ; pop two values: rax = first pushed, rdx = second pushed, rcx = rdx pop_two: dec r15 mov rdx, [stack + r15 * 8] dec r15 mov rax, [stack + r15 * 8] mov rcx, rdx ret
nasm -f elf64 -o calc.o calc.asm
ld -o calc calc.o
./calc 3 4 +
echo $?
7
0
Build steps
Loop over argv from 1 to argc. For each argument, compare the first byte against +, -, *, /. If it matches and the argument is one character, pop two operands, compute, and push the result. Otherwise convert the string to a qword and push it. After the loop, pop the final value, convert to decimal digits into a buffer, and sys_write it with a newline. Test with 2 3 '*', 10 4 -, and 100 7 / so you check the quotient path too.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How does RPN write
3 + 4?Answer
3 4 +
Both operands come before the operator, which pops them.
-
What does the operator do with the stack?
Answer
It pops two values and pushes the result
A binary operator consumes its two operands and leaves one result.
-
How is a number argument converted from text?
Answer
Multiply the running value by 10 and add the digit
The classic loop parses "123" as ((1*10)+2)*10+3.
-
How is the result printed?
Answer
Repeated division by 10, digits stored backwards
Division by 10 gives the digits from the least significant one, so they are stored in reverse.
-
Why does the program check
byte [rsi + 1]before treating an argument as an operator?Answer
To require a one-character argument
Without the check, a number that starts with a sign could look like an operator.
-
What does the program print for
./calc 10 4 -?Answer
6
10 minus 4 is 6, and the result is printed with a newline.
-
What does the program print for
./calc 2 3 "*"?Answer
6
The operator multiplies the two pushed values.
-
What does the program print for
./calc 100 7 /?Answer
14
Integer division gives 14, and the remainder is dropped.
-
What does this program print?
default rel %define SYS_WRITE 1 %define SYS_EXIT 60 section .bss stack resq 64 outbuf resb 32 section .text global _start _start: pop r12 mov rbx, rsp xor r15, r15 dec r12 add rbx, 8 arg_loop: test r12, r12 jz finish mov rsi, [rbx] mov al, [rsi] cmp byte [rsi + 1], 0 jne number cmp al, "+" je op_add cmp al, "-" je op_sub cmp al, "*" je op_mul cmp al, "/" je op_div number: xor rax, rax xor rcx, rcx parse: movzx rdx, byte [rsi + rcx] test rdx, rdx jz push_value sub rdx, "0" imul rax, rax, 10 add rax, rdx inc rcx jmp parse op_add: call pop_two add rax, rdx jmp push_value op_sub: call pop_two sub rax, rdx jmp push_value op_mul: call pop_two imul rax, rdx jmp push_value op_div: call pop_two xor rdx, rdx div rcx jmp push_value push_value: mov [stack + r15 * 8], rax inc r15 add rbx, 8 dec r12 jmp arg_loop finish: dec r15 mov rax, [stack + r15 * 8] lea rdi, [outbuf + 31] mov byte [rdi], 10 dec rdi mov rcx, 10 digits: xor rdx, rdx div rcx add dl, "0" mov [rdi], dl dec rdi test rax, rax jnz digits inc rdi lea rdx, [outbuf + 32] sub rdx, rdi mov rsi, rdi mov rax, SYS_WRITE mov rdi, 1 syscall mov rax, SYS_EXIT xor rdi, rdi syscall pop_two: dec r15 mov rdx, [stack + r15 * 8] dec r15 mov rax, [stack + r15 * 8] mov rcx, rdx retThe program runs as./calc 5 6 +.Answer
11
The two numbers are pushed and the plus pops them, adds 5 and 6, and prints 11.
-
What exit status does the calculator return?
Answer
0
After printing, the program exits with rdi cleared.
The calculator was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev