Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdoe committed Jul 31, 2023
1 parent ee4deed commit 598bb2d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
Binary file added screenshots/game-350.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
106 changes: 106 additions & 0 deletions week-047.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,109 @@ assume x is on address 15:
![day-349-f.jpg](./screenshots/day-349-f.jpg "game 349 screenshot")
![day-349-g.jpg](./screenshots/day-349-g.jpg "game 349 screenshot")
![day-349-h.jpg](./screenshots/day-349-h.jpg "game 349 screenshot")


## [DAY-350] machine code; functions

imagine we want to write something like:

```
def printme(x)
print(x)
print(x)
x = 4
printme(x)
beep
```

how would we call a function, for this we will pretend our computer has more memory and can work with addressess that are 1 whole byte (from 0 to 255).

first lets write our function, assuming the parameter will come from the R0 register: `11 X 8 _ 11 X 8 _`, we will write the value of R0 into the print instruction, lets write it at address 40:
and the rest of the program we will write at address 0, which is loading the value of X in the R0 register and then jumping to addr 40 to execute the function.

```
┌────┬────┬────┬────┐
0 │ 9 │ 15 │ 13 │ 40 │
├────┼────┼────┼────┤
4 │ 7 │ 0 │ 0 │ 0 │ (attempt to beep after the jump)
├────┼────┼────┼────┤
8 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
12 │ 0 │ 0 │ 0 │ 4 │ <- x is at addr 15
├────┼────┼────┼────┤
16 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
20 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
24 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
28 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
32 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
36 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
40 │ 11 │ 43 │ 8 │ 0 │
├────┼────┼────┼────┤
44 │ 11 │ 47 │ 8 │ 0 │
├────┼────┼────┼────┤
48 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
52 │ 0 │ 0 │ 0 │ 0 │
....
├────┼────┼────┼────┤
│ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
│ 0 │ 0 │ 0 │ 0 │
└────┴────┴────┴────┘
```


now you can see we can call the function, but we cant go back in order to beep, so what we will do is in R1 we will store which address we want to go back after the function is done.

```
┌────┬────┬────┬────┐
0 │ 9 │ 15 │ 10 │ 6 │ <- load the value of addr 8 into R1,
├────┼────┼────┼────┤ so we can go back to wherever it points
4 │ 13 │ 40│ 8 │ 7 │ <- we want to go back to addr 8
├────┼────┼────┼────┤ where we have the beep instruction
8 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
12 │ 0 │ 0 │ 0 │ 4 │ <- x is at addr 15
├────┼────┼────┼────┤
16 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
20 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
24 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
28 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
32 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
36 │ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
40 │ 11 │ 43 │ 8 │ 0 │
├────┼────┼────┼────┤
44 │ 11 │ 47 │ 8 │ 0 │
├────┼────┼────┼────┤
48 │ 12 │ 51 │ 13 │ 0 │ <- use R1 to jump back
├────┼────┼────┼────┤
52 │ 0 │ 0 │ 0 │ 0 │
....
├────┼────┼────┼────┤
│ 0 │ 0 │ 0 │ 0 │
├────┼────┼────┼────┤
│ 0 │ 0 │ 0 │ 0 │
└────┴────┴────┴────┘
```

so you can see we store


![day-350.jpg](./screenshots/day-350.jpg "game 350 screenshot")

0 comments on commit 598bb2d

Please sign in to comment.