about summary refs log tree commit diff stats
path: root/subx/gen
blob: 91370ea419a880dede40a805a2214f42a54132ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env zsh
# Build commonly-used SubX programs.

if [[ $1 == 'ex'* ]]
then
  CFLAGS=-g ./subx translate examples/$1.subx -o examples/`echo $1 |sed 's/\..*//'`
  exit $?
fi

CFLAGS=-g ./subx translate *.subx apps/$1.subx  -o apps/`echo $1 |sed 's/\..*//'`
exit $?
Agaram <vc@akkartik.com> 2018-11-30 16:45:15 -0800 committer Kartik Agaram <vc@akkartik.com> 2018-11-30 16:45:15 -0800 4808 - clean up comments in all subx files' href='/akkartik/mu/commit/subx/examples/ex9.subx?h=hlt&id=9d27e966b5e9bf1bd3da48f49d7e133d112a2bbe'>9d27e966 ^
33352536 ^
9d27e966 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
33352536 ^



6030d7e2 ^
d9818a53 ^
03d50cc8 ^
33352536 ^





6030d7e2 ^
33352536 ^
6030d7e2 ^
ed0e64a9 ^
294a1520 ^

ee9a9237 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

                                         


                                                                            
         

                                                        




                                 


                     
       
                                    
 
                  


                                                                                                                                                 
 
      
              
                                                                                                                                                                       

                                        
                                                                                                                                                                      
                      
                                                                                                                                                                     
              
                                    
                      



                                                                                                                                                                       
                         
 
                                                             





                                                                                                                                                                            
         
                                                                                                                                                                             
             
 

                  
                            
# Example showing arg order on the stack.
#
# Show difference between ascii codes of first letter of first arg and first
# letter of second arg.
#
# To run:
#   $ ./subx translate examples/ex9.subx -o examples/ex9
#   $ ./subx run examples/ex9 z x
# Expected result:
#   $ echo $?
#   2
#
# At the start of a SubX program:
#   argc: *esp
#   argv[0]: *(esp+4)
#   argv[1]: *(esp+8)
#   ...
# Locals start from esp-4 downwards.

== 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

Entry:
    # . prolog
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # ascii-difference(argv[1], argv[2])
    # . . push argv[2]
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
    # . . push argv[1]
    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
    # . . call
    e8/call  ascii-difference/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # syscall(exit, eax)
    89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
    b8/copy-to-eax  1/imm32/exit
    cd/syscall  0x80/imm8

ascii-difference:  # (s1, s2) : null-terminated ascii strings
    # a = first letter of s1 (ecx)
    8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none              0/r32/eax   4/disp8         .                 # copy *(esp+4) to eax
    8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           0/r32/eax   .               .                 # copy *eax to eax
    # b = first letter of s2 (edx)
    8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none              1/r32/ecx   8/disp8                           # copy *(esp+8) to ecx
    8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           1/r32/ecx   .               .                 # copy *ecx to ecx
    # a-b
    29/subtract                     3/mod/direct    0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # subtract ecx from eax
    c3/return

== data 0x0a000000

# . . vim:nowrap:textwidth=0