summaryrefslogtreecommitdiff
path: root/04
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-01-08 10:15:43 -0500
committerpommicket <pommicket@gmail.com>2022-01-08 10:15:43 -0500
commitd74f349e27a1ba20bd549362f95915ee6d2dd9d1 (patch)
tree900ab7d53c215e47c5bfb83acea94e1c74e3a19e /04
parent262824b21491446bb20acba8be1054207b5f50f0 (diff)
allow terms to be more complicated
Diffstat (limited to '04')
-rw-r--r--04/README.md6
-rw-r--r--04/guessing_game4
-rw-r--r--04/in03104
-rw-r--r--04/in047
4 files changed, 56 insertions, 65 deletions
diff --git a/04/README.md b/04/README.md
index b9ee066..68cd81d 100644
--- a/04/README.md
+++ b/04/README.md
@@ -227,6 +227,9 @@ A *term* is one of:
- `{variable name}` - the value of a (local or global) variable
- `.{label name}` - the address of a label
- `{number}`
+- `&{variable}` - address of variable
+- `*1{variable}` / `*2{variable}` / `*4{variable}` / `*8{variable}` - dereference 1, 2, 4, or 8 bytes
+- `~{term}` - bitwise not
An *l-value* is the left-hand side of an assignment expression,
and it is one of:
@@ -239,9 +242,6 @@ and it is one of:
An *r-value* is an expression, which can be more complicated than a term.
r-values are one of:
- `{term}`
-- `&{variable}` - address of variable
-- `*1{variable}` / `*2{variable}` / `*4{variable}` / `*8{variable}` - dereference 1, 2, 4, or 8 bytes
-- `~{term}` - bitwise not
- `{function}({term}, {term}, ...)`
- `{term} + {term}`
- `{term} - {term}`
diff --git a/04/guessing_game b/04/guessing_game
index 2c8fea9..5c85252 100644
--- a/04/guessing_game
+++ b/04/guessing_game
@@ -107,12 +107,10 @@ function stoi
function strlen
argument s
- local c
local p
p = s
:strlen_loop
- c = *1p
- if c == 0 goto strlen_loop_end
+ if *1p == 0 goto strlen_loop_end
p += 1
goto strlen_loop
:strlen_loop_end
diff --git a/04/in03 b/04/in03
index 63cbbbc..99b3c6e 100644
--- a/04/in03
+++ b/04/in03
@@ -1238,6 +1238,12 @@ align
?C=D:term_number
D='.
?C=D:term_label
+ D='*
+ ?C=D:term_dereference
+ D='&
+ ?C=D:term_addressof
+ D='~
+ ?C=D:term_bitwise_not
D=d58
?C<D:term_number
; (fallthrough)
@@ -1274,16 +1280,6 @@ align
C=:rvalue
8C=I
- C=1I
- D='&
- ?C=D:rvalue_addressof
-
- D='~
- ?C=D:rvalue_bitwise_not
-
- D='*
- ?C=D:rvalue_dereference
-
J=I
:rvalue_loop
C=1J
@@ -1506,50 +1502,6 @@ align
:rvalue_shr
call :set_rcx_to_rsi
!:emit_shr_rax_cl
-
-:rvalue_addressof
- I+=d1
- !:set_rax_to_address_of_variable
-
-:rvalue_bitwise_not
- I+=d1
- call :set_rax_to_term
- J=d4
- I=:not_rax
- D=d3
- syscall x1
- return
-:not_rax
- x48
- xf7
- xd0
-
-:rvalue_dereference_size
- reserve d1
-
-:rvalue_dereference
- I+=d1
- D=1I
- C=:rvalue_dereference_size
- 1C=D
- I+=d1
- call :set_rax_to_variable
- call :set_rbx_to_rax
- call :zero_rax
- C=:rvalue_dereference_size
- C=1C
-
- D='1
- ?C=D:set_al_to_[rbx]
- D='2
- ?C=D:set_ax_to_[rbx]
- D='4
- ?C=D:set_eax_to_[rbx]
- D='8
- ?C=D:set_rax_to_[rbx]
-
- !:bad_term
-
; set <rax> to address of variable in rsi
:set_rax_to_address_of_variable
@@ -1600,7 +1552,51 @@ align
call :read_number
I=A
!:set_rax_to_immediate
+
+:term_bitwise_not
+ I+=d1
+ call :set_rax_to_term
+ J=d4
+ I=:not_rax
+ D=d3
+ syscall x1
+ return
+:not_rax
+ x48
+ xf7
+ xd0
+
+:term_dereference_size
+ reserve d1
+
+:term_dereference
+ I+=d1
+ D=1I
+ C=:term_dereference_size
+ 1C=D
+ I+=d1
+ call :set_rax_to_variable
+ call :set_rbx_to_rax
+ call :zero_rax
+ C=:term_dereference_size
+ C=1C
+ D='1
+ ?C=D:set_al_to_[rbx]
+ D='2
+ ?C=D:set_ax_to_[rbx]
+ D='4
+ ?C=D:set_eax_to_[rbx]
+ D='8
+ ?C=D:set_rax_to_[rbx]
+
+ !:bad_term
+
+:term_addressof
+ I+=d1
+ !:set_rax_to_address_of_variable
+
+
; set rax to the number in the string at rsi
:read_number
C=1I
diff --git a/04/in04 b/04/in04
index 2b85900..339355e 100644
--- a/04/in04
+++ b/04/in04
@@ -15,8 +15,7 @@ function strlen
local p
p = s
:strlen_loop
- c = *1p
- if c == 0 goto strlen_loop_end
+ if *1p == 0 goto strlen_loop_end
p += 1
goto strlen_loop
:strlen_loop_end
@@ -24,9 +23,7 @@ function strlen
function putc
argument c
- local p
- p = &c
- syscall(1, 1, p, 1)
+ syscall(1, 1, &c, 1)
return
function puts