diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2021-10-20 14:35:18 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2021-10-20 14:35:18 -0700 |
commit | 3dead5564151b9f3f02cac638b6e526082d9818b (patch) | |
tree | 6272eb281bf86d8dc5c393bb04004a893cd57793 /tutorial | |
parent | f263a2070b769a6365d442fd5f4a2fb6fefac3c8 (diff) | |
download | mu-3dead5564151b9f3f02cac638b6e526082d9818b.tar.gz |
task: variables in registers vs memory
Diffstat (limited to 'tutorial')
-rw-r--r-- | tutorial/index.md | 25 | ||||
-rw-r--r-- | tutorial/task4.mu | 2 | ||||
-rw-r--r-- | tutorial/task5-solution.mu | 18 | ||||
-rw-r--r-- | tutorial/task5.mu | 15 |
4 files changed, 59 insertions, 1 deletions
diff --git a/tutorial/index.md b/tutorial/index.md index ca840d0d..621d507a 100644 --- a/tutorial/index.md +++ b/tutorial/index.md @@ -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 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). + +## 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 +``` diff --git a/tutorial/task4.mu b/tutorial/task4.mu index ca8da8f4..786219cf 100644 --- a/tutorial/task4.mu +++ b/tutorial/task4.mu @@ -8,7 +8,7 @@ fn the-answer -> _/eax: int { fn test-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 { diff --git a/tutorial/task5-solution.mu b/tutorial/task5-solution.mu new file mode 100644 index 00000000..147cc77f --- /dev/null +++ b/tutorial/task5-solution.mu @@ -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 { +} diff --git a/tutorial/task5.mu b/tutorial/task5.mu new file mode 100644 index 00000000..eed8f03c --- /dev/null +++ b/tutorial/task5.mu @@ -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 { +} |