Skip to content

Commit 9b884e3

Browse files
authored
Adding param virtual instruction to label stack entries before call pc (#43)
* Adding `param` virtual instruction to label stack entries before call pc * add some comment explaining +1 * use spaces for indent * correct comment * power of 5 for a change and minor update
1 parent 344be3c commit 9b884e3

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ run: vm
3232
./vm compile -loglevel debug programs/incr.asm
3333
./vm run -loglevel debug programs/incr.vm
3434
./vm compile -loglevel debug programs/loop.asm
35+
./vm compile -loglevel debug programs/pow.asm
36+
./vm run -loglevel debug programs/pow.vm
3537
time ./vm run -profile-cpu cpu.pprof programs/loop.vm
3638

3739
GEN:=cpu/instruction_string.go cpu/syscall_string.go

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Assembler only:
3939
- `str8` for string (with the double or backtick quotes)
4040
- on a line preceding an instruction: _label_ + `:` label for the *R instruction (relative address calculation). _label_ starts with a letter.
4141
- `Var v1 v2 ...` virtual instruction that generates a `Push` instruction with the number of identifiers provided and defines labels for said variables starting at 0 (which will start with the value of the accumulator while the rest will start 0 initialized).
42+
- `Param p1 p2 ...` virtual instruction that generates stack labels for p1, p2 as offset from before the return PC (ie parameters pushed (via `Var` or `Push`) by the caller before calling `Call`)
4243
- `Return` virtual instruction that generates a `Ret n` where _n_ is such as a Var push is undone.
4344

4445
## Benchmarks

asm/asm.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ func compile(reader *bufio.Reader, writer *bufio.Writer) int {
237237
if narg != 0 {
238238
return log.FErrf("Expecting 0 arguments for return, got %d (%v)", narg, args)
239239
}
240-
case "var":
240+
case "var", "param":
241241
if narg == 0 {
242-
return log.FErrf("Expecting at least 1 argument for var, got none")
242+
return log.FErrf("Expecting at least 1 argument for %s, got none", instr)
243243
}
244244
case "incrr", "incrs", "sys", "syss", "storesb":
245245
if narg != 2 {
@@ -281,6 +281,14 @@ func compile(reader *bufio.Reader, writer *bufio.Writer) int {
281281
varmap[args[i]] = cpu.ImmediateData(i)
282282
}
283283
log.Debugf("Var -> Push %d and defined variables: %v", narg-1, varmap)
284+
case "param":
285+
// define more stack labels
286+
start := returnN + 1 // +1 to skip over the return PC
287+
for i := range narg {
288+
varmap[args[i]] = cpu.ImmediateData(start + i)
289+
}
290+
log.Debugf("Param -> Defined parameters: %v", varmap)
291+
continue
284292
case "return":
285293
data = false
286294
op = op.SetOpcode(cpu.Ret)

programs/pow.asm

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; Demo of a subroutine that takes more than 1 input argument
2+
; (note that one of the arguments would probably be fine in accumulator but passing both
3+
; via the stack is more demonstrative)
4+
loadi 5
5+
var base exp # accumulator (so: 2) pushed to stack slot 0, creates exp slot 1
6+
loadi 3
7+
stores exp
8+
call pow
9+
sys exit 0 ; note we leave the 2 variables in stack on exit which is fine for this demo
10+
11+
pow:
12+
; inside the pow subroutine, s0 is the return address so s1 is the first argument (base) and s2 is the second argument (exp)
13+
loadi 1 ; initialize result to 1
14+
var result ; now we pushed one more so exp is at slot 3 and base at slot 2
15+
param b e ; b = base, e = exp, labels for their positions in the stack before PC
16+
loop:
17+
loads result
18+
muls b
19+
stores result
20+
incrs -1 e
21+
jnz loop
22+
loads result
23+
return

0 commit comments

Comments
 (0)