Transcript from the "Stack & OpCode Solution" Lesson
>> One of the questions asked during the break was what happens to the actual opcode that we're using. So in this case, I 32 multiplier. So all opcodes inherently when they perform the operation, push the value onto the stack. Actually, let's pull this up in web assembly studio switch over here.
[00:00:21] What's in this case it's an add function, that's fine. So get local it's pushing that left hand side operator OR operator zero onto the stack. Get local. The next one is pushing the next parameter onto the stack. I 32 ad is going to pop both values off the stack.
[00:00:42] Add them together, and then return that. And by return, I mean it pushes that value onto the stack. So now, inherently if we return and when we return, it's just gonna return whatever the value on the stack is. But if we wanted to continue to do operations on in this module, We can say I32, we'll say const 1, so we'll add 1.
[00:01:08] And then I can say I32 add. And this is where it gets a little tricky once you get more complex because you have to remember that even though there's no variable here, you don't see an explicit equals or anything like that. There is a number on the stack already.
[00:01:26] And add is gonna take both numbers. So here's the number on a stack. Here's the next number on a stack. So this is gonna add the two numbers together and then add plus 1. In the next section, we'll see why we don't write web assembly by hand, because reasoning about this is really complicated, because mentally you have to keep track of okay.
[00:01:45] This operation puts the number into the stack. Okay, so that's that number and then we take this number, and then that takes two operands to perform the operation on, and then it gets really confusing. That's why all the examples we're doing today are very simple so we don't have to go too far down that rabbit hole, but I'm glad you asked.
[00:02:06] Okay. What was the example I said, minus one. All right, so how did everybody do, did you create a minus one function? And I'm cheating here, I already created it, I embedded the web assembly. So your function should look something like this. Again the export, it can be at the top or the bottom.
[00:02:27] By convention, a lot of exports are at the top just because it makes it easy when you look at a file to see what's being exported. That's just generally how it goes or the convention, but you can do whatever you want. You can name it minus one, which I did.
[00:02:41] I call the parameter x. So I'm just getting That parameter, I'm getting local. I can also say get local zero, it'll do the same thing cuz it's getting that first parameter. But again, I like to name things cuz it makes it easy to reason about. So we're pushing this number into the stack.
[00:02:57] We're declaring a number one, and then we're pushing that onto the stack and then we're subtracting the number. And then because the function will always return whatever's on the stack. This is implied the return is implied at this point. So if I run this I build it. The bill.
[00:03:15] Yes, it builds. I run it. It returns a two because I passed in a three