 This time, we're going to look at building a case statement in assembly language. So I've set myself up with this basic case statement where I'm just going to test some variable i, which I'll say is in t0. I'm going to be checking to see which of these four conditions it matches. So in this case, I'm not going to have something that looks like case. I'm actually just going to have a whole bunch of branch statements, each one of which goes off to some place where I could run whatever instructions are associated with that case. So for example, the first case, case 0, I'm going to say that if t0 is equal to $0, since $0 contains the value 0, then I will go to case 0. At the moment, I don't have the value 1 in a register, so I'll put the value 1 into t9, then I can say if t0 equals t9, go to 1, then I'll put the value 2 into t9, and then I can say if t0 equals 2, go to case 2. Otherwise, I will go to the default case. Now, I could have done something similar to what I did before where in between my branches, I had all the code to perform these operations, but using this format fits a little closer with what we think of as a case statement, and it also allows me to write my conditions the way I understand them. So now I'm testing for the case when t0 is equal to 0, or when t0 is equal to 1, or when t0 is equal to 2. Rather than testing for the case when t0 is not equal to 0, and then going down to make another test. Next, I need to go out all of the code to actually implement those cases. So for case 0, I need to print out the string 0, and since I haven't added those, I'll go to that now. Now, once I'm done with all of my code in my 0 case, I need to exit to the end of the case statement. So I'm going to add a jump instruction here to an end case label. I'll put my end case label down here at the very end after all of my other code. Now I can go back and add the next case. Next case is 1, and here we just print out the 1 string. Since this is the only thing I'm doing in my 1 case, I will also jump down to the end case label after printing that string. The 2 block is similar as is the default case. This time, though, I could just fall through to my end case because adding the jump to end case instruction will really just jump to the next line of code. Regardless of whether I use a jump end case or not, I'm really just going to end up at the next line of code. So to begin with, I've got the value of 0 in T0, so I should expect to print out the 0 string. So when I run this code, it does print out 0. If I put in 1 for T0, then it prints out 1. If I put in 2 for T0, it prints out 2. If I put in 3 for T0, then it prints out default, but it also prints out default if I put in, say, negative 1 for T0. So in this case, I just have branch instructions that match the condition I'm actually looking for, and they go off to some block that just runs whatever code is in that case, then goes back to some join point after the case statement. In this case, I'm just using branch equals instructions because I have a basic case statement, but I could easily do this with more complicated relations. I could compare, say, T0 to T1. If one is greater than the other, I could run the code, or I can do other similar things. So lastly, we're going to look at how that would apply to a regular if-else statement. Some students find this easier to do because you are writing the condition as you understand it rather than the reverse of the condition, but it ends up being a little clunky. You get a whole lot more code. The code that's actually being executed is starting to get away from where you're testing your condition. So it doesn't look quite as nice, but it can be easier to understand. For this one, I would start off with the test. So this time I would have branch equals T0 of T1 to the if condition, and then I could do branch not equals T0 of T1 else because that's the opposite. But really, I know that if I got past my branch equals instruction that T0 is not equal to T1. So I could just use a jump instruction instead. Now I need to put in my if condition and my if condition says to print out the string hello. Now that I'm done printing that, I want to jump out of my if-else statement. So I'll add a jump to end-if label. Then I have the else block, and here we'll also print out a different string. Now that I'm done, I'm just going to fall through to the end-if label and I'll run whatever code is after this statement. So I'll fill in two values for T0 and T1 here. In this case, T0 is equal to T1. So when I run this code, it prints out hello. If I change T1 to 1, then T0 is not equal to T1, so it prints out the word world instead. So this style is more similar to what we had with the case statement where we were checking for all of our conditions and then they branched off to some block of code where we actually implemented the case. Each of these blocks is separated as it's got its own label, and at the end of the block we jump back to some common case that we want to run afterwards. That's all we're really doing here. We just only have one case to test for. We're only testing for the case where T0 is equal to T1. And again, we could do this with something more complicated. We could have done the comparison for, say, T0 being less than T1 or T0 being greater than or equal to T1. That would have changed the comparison instruction, how we do the branch, but that's it. The rest of our conditional structure would be the same.