Capstone: command-line calculator

Contents
The goal
Design
Build steps

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:

section .bss stack resq 64 section .text ; push: [top] = value, top++ ; pop: top--, value = [top]

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.

Next: Capstone: hex editor

Article author: Arthur Isaev

Related articles
Assembler
Assembler course: zero to hero in 50 lessons
What is assembly language
Install NASM and check the version
NASM syntax basics
Your first program
Registers
MOV and data transfer
Number bases and operands
Defining strings and data
Flags and the status register
Conditional jumps
Loops
The stack
Procedures
LEA and addressing modes
Memory layout: sections
Linux syscalls
Arithmetic
Bitwise operations
Arrays
Structures
Macros
Calling C functions
Debugging with GDB
The ELF format
Time and date syscalls
File I/O
Command-line arguments
Environment variables
Floating point (SSE)
SIMD basics
Interrupts
Sockets
A tiny HTTP client
Output formats: bin, elf, elf64
Advanced macros and %include
Linking object files
32-bit vs 64-bit
The System V ABI
Makefiles for assembly
Optimization basics
Error codes and logging
Parsing CSV
Data structures: linked list
A tiny HTTP server
Boot sector: hello in 512 bytes
Capstone: command-line calculator
Capstone: hex editor
Performance: cycles and cache
Modern x86: AVX tour
Hero roadmap: the whole course on one page
Install NASM on Windows 11
Install NASM on Ubuntu
Install NASM on Rocky Linux
Install NASM on macOS
Install NASM on FreeBSD
Assembly in Docker
ARM assembly

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: