diff --git a/_posts/2024-10-30-coa-three.md b/_posts/2024-10-30-coa-three.md index 98dc678d657c..00d98b5dd9ec 100644 --- a/_posts/2024-10-30-coa-three.md +++ b/_posts/2024-10-30-coa-three.md @@ -122,15 +122,17 @@ LOAD A <-> AC <- A ### 0-address instructions -Use a stack to find the operands. See the example of c=a+b below: +Use a stack to find the operands. See the example of c=a-b below: -``` +```asm push a push b -add // pop a and b from stack and add them, push the result to stack +sub ; pop a and b from stack and add them, push the result to stack pop c ``` +The operation command find the operands from the top of the stack. And the first operand is below the second operand. + ## Instruction set design ## Reverse Polish Notes @@ -147,6 +149,81 @@ pop c **Answer:** +- For zero address instructions: + +```asm +PUSH B +PUSH C +MUL +PUSH A +ADD +PUSH D +PUSH E +PUSH F +MUL +SUB +DIV +``` + +- For one address instructions: + +```asm +LOAD B ; Load B into register +MUL C + +STORE TEMP1 ; Store the result of multiplication in TEMP1 register + +LOAD A +ADD TEMP1 + +STORE TEMP2 + +LOAD E +MUL F + +STORE TEMP3 + +LOAD D +SUB TEMP3 + +STORE TEMP4 + +LOAD TEMP2 +DIV TEMP4 + +STORE RESULT +``` + +- For two address instructions: + +```asm +MOVE R1, B ; Load B into reg R1 +MUL R1, C ; R1 = B * C + +MOV R2, A ; Load A into reg R2 +ADD R2, R1 ; R2 = A + (B * C) + +MOVE R3, E +MUL R3, F ; R3 = E * F + +MOVE R4, D +SUB R4, R3 ; R4 = D - (E * F) + +DIV R2, R4 ; R2 = (A + B * C) / (D - E * F) + +MOVE RESULT, R2 +``` + +- For three address instructions: + +```asm +MUL R1, B, C ; R1 = B * C +ADD R2, A, R1 ; R2 = A + (B * C) +MUL R3, E, F ; R3 = E * F +SUB R4, D, R3 ; R4 = D - (E * F) +DIV RESULT, R2, R4 ; RESULT = (A + B * C) / (D - E * F) +``` + **Question 12.18:**
![Question 12.18 ](/assets/img/COA-images/homework/12-18.png){: .mx-auto.d-block :}