1 # Example showing arg order on the stack. 2 # 3 # Show difference between ascii codes of first letter of first arg and first 4 # letter of second arg. 5 # 6 # To run (from the subx directory): 7 # $ ./subx translate examples/ex9.subx -o examples/ex9 8 # $ ./subx run examples/ex9 z x 9 # Expected result: 10 # $ echo $? 11 # 2 12 # 13 # At the start of a SubX program: 14 # argc: *ESP 15 # argv[0]: *(ESP+4) 16 # argv[1]: *(ESP+8) 17 # ... 18 # Locals start from ESP-4 downwards. 19 20 == code 0x09000000 21 # instruction effective address register displacement immediate 22 # . op subop mod rm32 base index scale r32 23 # . 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 24 25 Entry: 26 # . prolog 27 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . . # copy ESP to EBP 28 # ascii-difference(argv[1], argv[2]) 29 # . . push argv[2] 30 ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12) 31 # . . push argv[1] 32 ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8) 33 # . . call 34 e8/call ascii-difference/disp32 35 # . . discard args 36 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP 37 # syscall(exit, EAX) 38 89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX 39 b8/copy-to-EAX 1/imm32/exit 40 cd/syscall 0x80/imm8 41 42 ascii-difference: # (s1, s2) : null-terminated ascii strings 43 # a = first letter of s1 (ECX) 44 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 0/r32/EAX 4/disp8 . # copy *(ESP+4) to EAX 45 8b/copy 0/mod/indirect 0/rm32/EAX . . . 0/r32/EAX . . # copy *EAX to EAX 46 # b = first letter of s2 (EDX) 47 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none 1/r32/ECX 8/disp8 # copy *(ESP+8) to ECX 48 8b/copy 0/mod/indirect 1/rm32/ECX . . . 1/r32/ECX . . # copy *ECX to ECX 49 # a-b 50 29/subtract 3/mod/direct 0/rm32/EAX . . . 1/r32/ECX . . # subtract ECX from EAX 51 c3/return 52 53 == data 0x0a000000 54 55 # . . vim:nowrap:textwidth=0