## Copyright (C) 2017 Jeremiah Orians
## This file is part of stage0.
##
## stage0 is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## stage0 is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with stage0.  If not, see <http://www.gnu.org/licenses/>.

## stage0's hex2 format for x86
##    !<label>          1 byte relative
##    $<label>          2 byte address
##    @<label>          2 byte relative
##    %<label>          4 byte relative
##    &<label>          4 byte address
##    label_<label>     function-local
##    string_<count>    string
##

#.text

:_start
55 89 e5 83 ec 40
e8 %main 83 c4 00
89 45 fc
# exit(r);

ff 75 fc
e8 %exit 83 c4 04
85 c0
c9 c3

:_env
55 89 e5 83 ec 40
8b 45 08
c9 c3

:exit
55 89 e5 83 ec 40
# asm(".byte 0x8b 0x5d 0x08");

8b 5d 08
# asm(".byte 0xb8 0x01 0x00 0x00 0x00");

b8 01 00 00 00
# asm(".byte 0xcd 0x80");

cd 80
c9 c3

:write
55 89 e5 83 ec 40
# asm(".byte 0x8b 0x5d 0x08");

8b 5d 08
# asm(".byte 0x8b 0x4d 0x0c");

8b 4d 0c
# asm(".byte 0x8b 0x55 0x10");

8b 55 10
# asm(".byte 0xb8 0x04 0x00 0x00 0x00");

b8 04 00 00 00
# asm(".byte 0xcd 0x80");

cd 80
c9 c3

:strlen
# while (s[i]) ...
55 89 e5 83 ec 40
c7 45 fc 00 00 00 00
e9 09 00 00 00
8b 45 fc
83 45 fc 01
85 c0
8b 45 fc
89 c2
8b 55 08
01 d0
0f b6 00
85 c0
85 c0
84 c0 74 05
e9 dd ff ff ff
8b 45 fc
c9 c3

:eputs
55 89 e5 83 ec 40
ff 75 08
e8 %strlen 83 c4 04
89 45 fc
# write(2, s, i);

ff 75 fc
ff 75 08
b8 02 00 00 00
50
e8 %write 83 c4 0c
85 c0
b8 00 00 00 00
c9 c3

#.data

:g_stdin
00 00 00 00

:g_environment
00 00 00 00
