UMBC CMSC 313 -- Expressions Previous | Next


Expressions

Going back to C, when you have an assignment statement, it is in the form of lvalue = expression; An rvalue is the adress of where to store the result of the expression. An expression is one term and possibly an operator and another expression. A term can be a constant: i = 3; Moving up the complexity ladder, we get: i = 3 + 4; Worse: i = 3 + 4 / 7; We can get into things like: i = a / b + 7 * 8 / 2 - 3 + c * d; The first two examples are no big deal. But for the third one, is the answer 1 or 3? How do we know? Precedence determines the sequencde that must be followed. The compiler does all of that stuff for you, but the assembler makes you do it. temp = 4 / 7; i = 3 + temp;

The last one becomes:

i = ((((a / b) + ((7 * 8) / 2)) - e) + (c * d)); Calculate a / b and put the results into temp1. i = (((temp1 + ((7 * 8) / 2)) - e) + (c * d)); // temp1 = a /b; Continuing:
i = (((temp1 + (temp2 / 2)) - e) + (c * d)); // temp2 = 7 * 8;
i = (((temp1 + temp3) - e) + (c * d));       // temp3 = temp2 / 2;
i = ((temp4 - e) + (c * d));                 // temp4 = temp1 + temp3;
i = (temp5 + (c * d));                       // temp5 = temp4 - e;
i = (temp5 + temp6);                         // temp6 = c * d;
i = temp7;                                   // temp7 = temp5 + temp6;

Put it all together:

;; temp1 = a /b;
;; temp2 = 7 * 8;
;; temp3 = temp2 / 2;
;; temp4 = temp1 + temp3;
;; temp5 = temp4 - e;
;; temp6 = c * d;
;; temp7 = temp5 + temp6;
;; i = temp7;
Now translate that into assembly language! It is possible to do this in less instructions, but a simple compiler would do it this way.


Previous | Next

©2004, Gary L. Burt