1 ## String comparison: return 1 iff the two args passed in at the commandline are equal. 2 # 3 # To run: 4 # $ subx translate ex10.subx ex10 5 # $ subx run ex10 abc abd 6 # Expected result: 7 # $ echo $? 8 # 0 # false 9 10 == code 11 # instruction effective address operand displacement immediate 12 # op subop mod rm32 base index scale r32 13 # 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 14 15 # main: return argv_equal(argv[1], argv[2]) 16 # At the start of a SubX program: 17 # argc: *ESP 18 # argv[0]: *(ESP+4) 19 # argv[1]: *(ESP+8) 20 # ... 21 # s1 = argv[1] (EAX) 22 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none . 0/r32/EAX 8/disp8 . # copy *(ESP+8) to EAX 23 # s2 = argv[2] (EBX) 24 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none . 3/r32/EBX 0xc/disp8 . # copy *(ESP+12) to EBX 25 # call argv_equal(s1, s2) 26 # push args 27 50/push-EAX 28 53/push-EBX 29 # call 30 e8/call argv_equal/disp32 31 # exit(EAX) 32 $exit: 33 89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX 34 b8/copy . . . . . . . 1/imm32 # copy 1 to EAX 35 cd/syscall 0x80/imm8 36 37 # compare two null-terminated ascii strings 38 # reason for the name: the only place we should have null-terminated ascii strings is from commandline args 39 argv_equal: # (s1, s2) : null-terminated ascii strings -> EAX : boolean 40 # initialize s1 (ECX) and s2 (EDX) 41 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none . 1/r32/ECX 8/disp8 . # copy *(ESP+8) to ECX 42 8b/copy 1/mod/*+disp8 4/rm32/sib 4/base/ESP 4/index/none . 2/r32/EDX 4/disp8 . # copy *(ESP+4) to EDX 43 # while (true) 44 $argv_loop: 45 # c1/EAX, c2/EBX = *s1, *s2 46 b8/copy 0/imm32 # clear EAX 47 8a/copy 0/mod/indirect 1/rm32/ECX . . . 0/r32/EAX . . # copy byte at *ECX to lower byte of EAX 48 bb/copy 0/imm32 # clear EBX 49 8a/copy 0/mod/indirect 2/rm32/EDX . . . 3/r32/EBX . . # copy byte at *EDX to lower byte of EBX 50 # if (c1 == 0) break 51 3d/compare . . . . . . . 0/imm32 # compare EAX with 0 52 74/jump-if-equal $argv_break/disp8 53 # if (c1 != c2) return false 54 39/compare 3/mod/direct 0/rm32/EAX . . . 3/r32/EBX . . # compare EAX with EBX 55 75/jump-if-not-equal $argv_fail/disp8 56 # ++s1, ++s2 57 41/inc-ECX 58 42/inc-EDX 59 # end while 60 eb/jump $argv_loop/disp8 61 $argv_break: 62 # if (c2 == 0) return true 63 81 7/subop/compare 3/mod/direct 3/rm32/EBX . . . . . 0/imm32 # compare EBX with 0 64 75/jump-if-not-equal $argv_fail/disp8 65 b8/copy . . . . . . . 1/imm32 # copy 1 to EAX 66 c3/return 67 # return false 68 $argv_fail: 69 b8/copy . . . . . . . 0/imm32 # copy 0 to EAX 70 c3/return