Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have to cacluate the given expression
F= (A+B)^2*C-A
on a 0,1,2,3 address machine.
I need help for 0 address machine , and if the code for other machines are correct ?

What I have tried:

0 address machine
     Need help ??????

   1 address machine
       LOAD A
       ADD B
       STORE TEMP
       MUL TEMP
       MUL C
       SUB A
       STORE F

   2 address machine
       MOVE F, A
       ADD F, B
       MUL F, F
       MUL F, C
       SUB F, A

     3 address machine
       ADD F, A, B
       MUL F, F, F
       MUL F, F, C
       SUB F, F, A
Posted

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

At the moment, we have to make assumptions about your various "machines" which may or may not be correct - I understand several different real-world assemblers which are targeted at specific processors (or processor families) and they all work in different ways: code that works for one will fail badly in another. You need to read what your tutor gave you as the "machine code definition" for each of your "machine types" in order to get working code for them. While it looks like your three code should work, that involves some pretty big assumptions which may not be in any way valid for your virtual machines.
For example, at least two of the real world processor families I can code for do not have a multiply instruction at all, and one of them has conditional execution for every single instruction! The assembly code for these would be wildly different from your examples!

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Given that you already have a partial answer to the three address machine case then this is what I would expect from you (adapted for the processor architecture in question) :

ASM
LOAD A, R1
LOAD B, R2
ADD R1, R2, R3
MUL R3, R3, R4
LOAD C, R5
MUL R4, R5, R6
SUB R6, R1, R7


1) LOAD A, R1: Load the value of A into register R1.
2) LOAD B, R2: Load the value of B into register R2.
3) ADD R1, R2, R3: Add the values in registers R1 and R2 and store the result in register R3.
4) MUL R3, R3, R4: Multiply the value in register R3 by itself and store the result in register R4.
5) LOAD C, R5: Load the value of C into register R5.
6) MUL R4, R5, R6: Multiply the values in registers R4 and R5 and store the result in register R6.
7) SUB R6, R1, R7: Subtract the value in register R1 from the value in register R6 and store the result in register R7.
 
Share this answer
 
Comments
OriginalGriff 6-Jan-24 8:30am    
This is why I talked about assumptions: every assembler I've used works the other way:
Operation Destination,Source
That's true for PIC, Z80, 8086(and successors), ARM, ... :D
jeron1 6-Jan-24 12:07pm    
This is true.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900