 This is like, so key three. Oh man, it's been a long time since I looked at ARM, but all right. But I do know the LR register is definitely the link register, which stores the address that you're gonna go back to. So we should be able to, so I think key one is easy, so let's create some kind of, so we think, at least now we think key one should return the program counter and the program counter for key one is either going to be, I believe it's gonna be the next instruction to be executed, so I'm gonna guess that it's this. Key three, three should be the link register, the LR, key three, move the LR, so that will be wherever key three is called the next instruction, so that should be this. And then, of course we add it together, okay, so key two then is an interesting one, so okay, so let's look at key two, so push R6 is R6, is there anything? The other thing is, ARM has a bunch of aliases for registers, oh, check out ARM, let's say the thing we wanted, ARM, all right, so push R6 onto the stack, add the program counter plus, oh, oh, oh, I see, so this push is, it must be the calling convention because it wouldn't need to store the values that we get in there, oh man, interesting. Okay, good, so link register is good, okay, so that tells us that. Now we go back here and we look, so we're storing the value, so we're gonna pop R6, so that will, basically it's just store whatever's in the value there, so we're gonna add one to the program counter and store that into R6, so that will be this value, we're going to BX, what the heck, got a branch there, okay, interesting. What is the X? Branch with link, branch and exchange, instruction set, so BLX will, BX will change the processor state from ARM to thumb, so this is an insane part about ARM that I don't fully understand, but we will dive right into it. I guess can I start up an ARM, yeah, but getting that file out will be difficult, but I mean, I have the C code, so I could try to just compile this on an ARM machine, which is not a bad way to go, but hey, I'm in pretty deep, so I might as well keep going here. So BX2, that register six, so that's the program counter, so that would be this plus one, man, so that maybe goes here, do I have anything, S trace, L trace, any kind of trace, oh, HLS, what do I have in bin? These are all busybox stuff, which I don't have a ton of experience with, let's see, Rp64, busybox, oh, I can, we could do that, let's do base 64 dash D leg, so you'll, oh, I can't actually read it, interesting, interesting, oh, okay, the permissions on leg means that I can only execute it, I can't actually read it. Do I have a proc file system? I do have a proc file system, I could probably do some stuff in there, but all right, so add PC to one, store it into X, man, that means it's gonna turn into thumb mode, and it's gonna be something with thumb mode, but now what is it doing? So let's walk through, so move the program counter into R3, add four to the program counter, what's an add S, let's just think it adds, adds four into R3 and stores it into R3, so that should be, so that should then push R3 onto the stack, what, how come it didn't push R3 onto, oh, oh, oh, oh, I see, so it's pushing R3 what we have in here onto the stack, and then popping that R3 into the program counter, so this should be, so program counter, so it should be 8DO4, 8DO4 plus R3, plus four, let's go to our calculator, handy dandy, if you're using 8 plus four, no, so the PC, I believe this should be six, although I should probably verify that at some point, so it should be the next instruction to be executed, man, it's crazy that you can, all right, plus four, 8DOA, so that means when we pop into PC, it looks like it's gonna be an infinite loop, but it will probably not be, well, the next thing after that would be popped, but that doesn't make any sense, so what happens when we get out of this, pop R6, move R3 into R0, so we wanna see what's in the PC register to understand is it the current address that's executing or is it going to be the next one, so let's see, in the arm state value of the PC is the address, the current instruction, plus eight bytes, interesting, in thumb, that is the current instruction plus four, for all other instructions that use labels value of the PC is the address, the current instruction plus four bytes, value of the PC order points to the instruction after the next instruction, oh, perfect, okay, that makes much more sense, which means we were right when we moved the PC into here, let's actually check that we have that in here, so key one should be C0 and key three, the LR, yeah, that points to the instruction after the next instruction, okay, it should be plus eight bytes, so let's see, all right, let's do this by hand, this calculation, this plus eight, E4, interesting, hmm, it's a nasty bit of legacy abstract, so you could understand a three-stage pipeline fetch, you could execute some of that design, they chose that the PC read is a value currently in the instruction fetch address line rather than that the currently executing instruction line two cycles ago, this most PC read address is architecting the link time, interesting, okay, so it will be plus eight, so it would be this address, so key one will return this value, now what is in the LR, that should be the actual next instruction to be executed, so we branch and link, the idea is RBL instruction, so BL, all right, the branch BL instruction causes a branch to label and copies the address of the next instruction into LR, the link register, awesome, so key three is correct because this is the next instruction that should be executed and key three is moving that back, ah, okay, so it's like they're getting into the difference, ooh, what's the difference between add and add s, good, it's almost like what they're doing here is they're, where was that, BX, no, I wanna go, ah, what's in the PC register, okay, great, and then the link register, okay, so because it's gonna flip it to thumb state, although just key three ever bring it back, or key two, does it ever bring it back to the regular state, so BX, ah, I don't know, what's BL branch and link, and all right, so take the PC, which if we're correct will be this, plus four, plus four, so it'll be this one, plus one though, so the question is will that do a four addition, add one, two, PC, arm, man, I really need something to debug this, I may just go install, okay, let's look at this add s thing, ah, add s, updates flags, well, that's useless, at least for our purposes, but it's good to know, let's see, adding to the PC, so this is the thing I don't really understand, so it's taking this, so our thoughts about this is correct, then this getting this PC should be this value, plus one, but that would be a bad offset, so that doesn't make any sense, and then branch exchange on that, which maybe will be here, add four to R3, push it, pop it, but R3 hasn't been anything yet, hmm, I still think for some reason it's gonna go here, and so it's really in R3 is gonna be four plus, let's see this value maybe, so it may be like this, let's see, can we see, the C code here is not very useful, oh, maybe it is, okay, let's see, PC.code16, move the PC to R3, add four to R3, push it, pop it, pop R6, so R6 would be this, the start of thumb mode, oh that makes sense, because each of these values is two, so we have 16, and 20, 22, 24, 26, 28, so I think that's gonna jump here, let's say yes, okay, so that makes sense, so this would be, R3 would be two, then what did it say for, what did that random person on Stack Overflow say, they said, ah, so this is actually from the instructions, for B, BL, CNZ, CNZ, the value of the PC is the address of the current instruction plus four bytes, for all the instructions that use labels, the value of the PC is the address of the current instruction plus four bytes, with bit one of the result clear to zero to make it word aligned, all right, so assuming we are in 32 bit, thumb mode, it is not a CBN, CBZ, BL, current address plus four bytes, so this would be this plus four, this plus four, which should be this plus four, oh eight, and then that gets added to again, added to four plus four again, and this, that doesn't make any sense, I mean, maybe that's what's in there, all right, let's see. All right, shall we try this, so we have, what we want is all these values added together, so we have zero D, zero eight, plus, I'm just gonna copy it from here, plus that should be something like this, so maybe this value will work, who actually knows this is actually a bit crazy, I have a strong leg, that was not correct, okay. All right, key returning integers, they're adding integers, all right, I'm going to pause this and I'm gonna try to figure out how to install a 32 bit, or how to install an ARM processor on here so I can try compiling this and running it, so all right, I'll be back. All right folks, I'm back, so after a fruitless search to actually try to get a ARM instance running, I went and tried multiple avenues, I tried using EC2, I figured hey, why not just pay to have somebody run it, it turns out at least, I wasn't able to find easily an Amazon ARM instance on EC2 that I could just fire up, I started looking at other types of things, I even, as you can see here, I started playing around and in my instance here, I started adding the cross-compiler so I could start running different types of programs and start trying to test things here so I could try to run it locally, unfortunately for some reason it was looping here and so this made me think, hey, maybe there's something weird going on that I don't understand which is definitely true, so I went back and I was still kind of confused about the, I figured from going through this and looking at key one, key one, so looking back at the C code, right, key one and key three are so simple that we pretty much, this has got to be it, so key one should return the program counter, key three should be the link register and so that leaves us with key three, so going through this again and really digging in, so once again, when you look at these kind of code and especially for if you're not very familiar with something, like if you're not familiar with x86, you're not familiar with ARM, like me in this case, what you've got to do is you've got to go in and you've got to become the X, so you have to read the documentation for each of these instructions so that you actually understand it and one thing that kept tripping me up is why is this code adding one to the program counter? I didn't really understand and then it kind of hit me, well ARM is a fixed byte offset, so why would it add one to it? It probably ignores it, so how does this branch instruction work? So then I actually, you know, went, googled this instruction and started reading it and said, so it says the BX instruction branches to the address contained in a specific register, the value of bit zero of the branch address determines whether the execution continues in ARM state or thumb state, so that's really interesting. It can be used this way because all ARM instructions are word aligned, that means bits zero and one of the address are ignored, so that was actually kind of my thought and my instinct there. And then we can see that it will tell us if, there we go, if bit zero is set, the instruction at the branch address is executed in thumb state, so we'd have to go look at thumb, basically we can actually even see here that the dot code 16, I believe this is a directive to the assembler to tell the assembler, hey, these next instructions are in thumb mode. And so then if we, going through this again, we can see, okay, so what does this key to do? Well, it, and here we need the actual instruction, so let's look here at this assembler output that we have. Oh, I also tried all kinds of ways of trying to try to get the actual binary itself off so I could start playing with it offline and maybe testing it and debugging it, all of those failed. So we have the prologue here, we save R6, then we take the program counter. So again, we know that the program counter here will be 8DO4, so we know, we go back to what we have here, 8DO4, that should be the program counter value that's in our, yes, that is in R6 right here. And then we take that add one, so remember that adding one is gonna make sure that the thumb mode is set and then we're gonna BX, so we're gonna branch and exchange the mode if that bit is set to R6 and we know R6 is at 8DO4, so that's where we are here, so this is great. So that now that adding one makes sense there and now that sounds really good. And so now we know we're executing just from this next instruction, so we, but we're executing in thumb mode, so we move the PC, move it into R3, so the PC should be two instructions before, so the PC here should be this value, 8DO4, it is then gonna move that into R3, it's then gonna add four to that value, so plus four, push that and actually we wanna see what is this actual value, so let's go and ask our handy-dandy calculator, so this would be 8DOC and so now when we pop it, we will actually pop and go here, so we'll push R3, pop what's on the stack into the program counter, which will cause us to start executing here at pop R6, then we're gonna move R3 into R0, so this is going to be the return of the function, so now what's the value in R0? Well, it's this handy-dandy value here, so this should be the return of key two and it looks like we were four bytes off, so that's kinda funny. And so now if we do this, plus this, plus this, base, turn it into base 10, go over here, execute leg, daddy has a very strong arm, congrats, my daddy has a lot of arm V5TE muscle. So it turns out this was, I was making this more complicated than it was, the leg stuff was kinda throwing me off, but so we can see there it really comes down to knowing what is arm, what is it supposed to do, how is it supposed to work, so thanks folks and I'll see you next time.