typestar

mul and div in Assembly

Multiplication and division live in rax; rdx catches the rest.

; mul and div work through rax; rdx catches high bits and remainder.
        global _start

        section .text
_start:
        mov rax, 6
        mov rbx, 7
        imul rax, rbx           ; rax = 42

        xor rdx, rdx            ; clear high half before dividing
        mov rcx, 5
        div rcx                 ; rax = 8, rdx = 2

        add rax, rdx            ; 10
        mov rdi, rax
        mov rax, 60             ; exit code 10
        syscall

How it works

  1. imul rax, rbx multiplies in place, signed.
  2. div needs rdx cleared first — it divides rdx:rax.
  3. Quotient lands in rax, remainder in rdx: 8 and 2.

Keywords and builtins used here

The run, in numbers

Lines
17
Characters to type
365
Tokens
46
Three-star pace
55 tpm

At the three-star pace of 55 tokens a minute, this run takes about 50 seconds.

Type this snippet

Step 2 of 3 in Arithmetic, step 5 of 31 in Language basics.

← Previous Next →