mu/examples/ex3.subx

40 lines
1.3 KiB
Plaintext
Raw Normal View History

# Add the first 10 numbers, and return the result in the exit code.
2018-07-24 18:53:45 +00:00
#
2019-08-20 06:45:04 +00:00
# To run:
2018-12-30 08:36:07 +00:00
# $ ./subx translate examples/ex3.subx -o examples/ex3
# $ ./subx run examples/ex3
# Expected result:
# $ echo $?
# 55
== code 0x09000000
# instruction effective address register displacement immediate
# . op subop mod rm32 base index scale r32
# . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes
2018-11-26 08:26:20 +00:00
Entry:
# result: ebx = 0
bb/copy-to-ebx 0/imm32
# counter: ecx = 1
b9/copy-to-ecx 1/imm32
$loop:
2019-02-15 00:24:20 +00:00
# if (counter > 10) break
81 7/subop/compare 3/mod/direct 1/rm32/ecx . . . . . 0xa/imm32 # compare ecx
7f/jump-if-greater $exit/disp8
# result += counter
01/add 3/mod/direct 3/rm32/ebx . . . 1/r32/ecx . . # add ecx to ebx
# ++counter
41/increment-ecx
# loop
eb/jump $loop/disp8
$exit:
# syscall(exit, ebx)
b8/copy-to-eax 1/imm32/exit
cd/syscall 0x80/imm8
== data 0x0a000000
# . . vim:nowrap:textwidth=0