BCPL and virtual machine things we saw today. No bytes. Memory is organised as 32 bit words. How the contents of a word is interpreted depends upon its context. a + b the 32 bits in a and b are treated as ints a #+ b the 32 bits in a and b are treated as floats a ##+ b the 32 bits in a and b are treated as unsigned ints a(...) the 32 bits in a are treated as the address of the first insrtuction of a function ! a the 32 bits in a are treated as a pointer to be followed. The 32 bits of an instruction have four parts 7 bits the opcode, what operation the instruction performs 1 bit if set, the operand is in memory somewhere 4 bits primary register 4 bits secondary register 16 bits numeric part of operand in LOAD R2, 65 the opcode is 1 for load indirect bit is zero primary register is R2 no secondary register numeric part is 65 R2 is set to 65 in MUL R4, R3 the opcode is 8 for multiply indirect bit is zero primary register is R4 secondary register is R3 numeric part is zero R4 is set to the value in R3 in MUL R4, R3 + 2 the opcode is 8 for multiply indirect bit is zero primary register is R4 secondary register is R3 numeric part is 2 R4 is set to 2 plus the value in R3 in MUL R4, [R3] the opcode is 8 for multiply indirect bit is one (square brackets) primary register is R4 secondary register is R3 numeric part is zero the value in R3 is treated as a memory address R4 is set to whatever is stored there in MUL R4, [R3 - 5] the opcode is 8 for multiply indirect bit is one (square brackets) primary register is R4 secondary register is R3 numeric part is -5 the value in R3 is treated as a memory address R4 is set to whatever is stored 5 words before that in STORE R4, [R3 - 5] the opcode is 8 for multiply indirect bit is one (square brackets) primary register is R4 secondary register is R3 numeric part is -5 the value in R4 is stored in memory 5 words before the address that is stored in R3. in INC R1 the opcode is 4 for increment indirect bit is off no primary register secondary register is R1 numeric part is zero 1 is added to the value stored in R1 in PUSH [ 23 ] the opcode is 34 for push indirect bit is one [square brackets] no primary register no secondary register numeric part is 23 The value found in memory location 23 is stored again in the memory location whose address register SP contains and 1 is subtracted from the value of the SP register On calling a function: The parameters are evaluated in reverse order and pushed onto the stack two times the number of parameters is pushed onto the stack the CALL F instruction pushes the value of the PC register (what would have been the next instruction to be executed) as the return address, and the PC is set to F. The first the function does at instruction F is to push the frame pointer, register FP, onto the stack and set FP equal to SP. The frame pointer always shows where the old frame pointer was saved. Then the number of local variables is subtracted from SP So during the life of a function call, the Nth parameter (counting from 1) will be stored at memory address FP + (N + 2) and the Nth local variable will be at FP - N. At the end of a function the value to be returned, if there is one, is stored in register R1. the stack pointer SP is set back to the value of the frame pointer FP, this has the effect of popping all of the local variables in one step. Then the old FP is popped from the stack to return to being the current FP value. Then a RET instruction pops the return address (the SP value that CALL pushed) and sets the PC equal to it, causing a jump back to where the CALL came from. Immediately after a CALL instruction, the function result, if there is one, is taken from R1 and stored wherever it is needed. Then the number of parameters plus one is added to the stack pointer, having the effect of popping all of the parameters and the two-times-their-number, and the program continues as though nothing had happened.