task: variables in registers vs memory

This commit is contained in:
Kartik K. Agaram 2021-10-20 14:35:18 -07:00
parent f263a2070b
commit 3dead55641
4 changed files with 59 additions and 1 deletions

View File

@ -167,3 +167,28 @@ ask questions or share your experience](http://akkartik.name/contact).
One gotcha to keep in mind is that numbers in Mu must always be in hexadecimal One gotcha to keep in mind is that numbers in Mu must always be in hexadecimal
notation, starting with `0x`. Use a calculator on your computer or phone to notation, starting with `0x`. Use a calculator on your computer or phone to
convert 42 to hexadecimal, or [this page on your web browser](http://akkartik.github.io/mu/tutorial/converter.html). convert 42 to hexadecimal, or [this page on your web browser](http://akkartik.github.io/mu/tutorial/converter.html).
## Task 5: variables in registers, variables in memory
We'll now practice managing one variable in a register (like last time) and
a second one in memory. To prepare for this, reread the first section of the
[Mu syntax description](https://github.com/akkartik/mu/blob/main/mu.md), and
then its section on [local variables](https://github.com/akkartik/mu/blob/main/mu.md#local-variables).
The section on [integer primitives](https://github.com/akkartik/mu/blob/main/mu.md#integer-primitives)
also provides a useful cheatsheet of the instruction forms you will need.
```
fn foo -> _/eax: int {
var x: int
# statement 1: store 3 in x
# statement 2: define a new variable 'y' in register eax and store 4 in it
# statement 3: add y to x, storing the result in x
return x
}
```
Again, you're encouraged to repeatedly try out your programs by running this
command as often as you like:
```
./translate tutorial/task5.mu && qemu-system-i386 code.img
```

View File

@ -8,7 +8,7 @@ fn the-answer -> _/eax: int {
fn test-the-answer { fn test-the-answer {
var result/eax: int <- the-answer var result/eax: int <- the-answer
check-ints-equal result, 0x2a, "F - the-answer should return 42, but didn't." check-ints-equal result, 0x2a, "F - the-answer should return 42, but didn't"
} }
fn main { fn main {

View File

@ -0,0 +1,18 @@
fn foo -> _/eax: int {
var x: int
# statement 1: store 3 in x
copy-to x, 3
# statement 2: define a new variable 'y' in register eax and store 4 in it
var y/eax: int <- copy 4
# statement 3: add y to x, storing the result in x
add-to x, y
return x
}
fn test-foo {
var result/eax: int <- foo
check-ints-equal result, 7, "F - foo should return 7, but didn't"
}
fn main {
}

15
tutorial/task5.mu Normal file
View File

@ -0,0 +1,15 @@
fn foo -> _/eax: int {
var x: int
# statement 1: store 3 in x
# statement 2: define a new variable 'y' in register eax and store 4 in it
# statement 3: add y to x, storing the result in x
return x
}
fn test-foo {
var result/eax: int <- foo
check-ints-equal result, 7, "F - foo should return 7, but didn't"
}
fn main {
}