Ch06 - Variables - Operations PDF
Ch06 - Variables - Operations PDF
Variables
• named, typed data items
Operators
Chapter 6 • predefined actions performed on data items
• combined with variables to form expressions, statements
Variables and
Operators Rules and usage
Implementation using LC-3
12-2
12-3 12-4
1
Examples Literals
Legal Integer
i 123 /* decimal */
wordsPerSecond same identifier
-123
words_per_second
_green 0x123 /* hexadecimal */
aReally_longName_moreThan31chars Floating point
aReally_longName_moreThan31characters
6.023
6.023e23 /* 6.023 x 1023 */
Illegal
5E12 /* 5.0 x 1012 */
10sdigit
ten'sdigit Character
done? reserved keyword 'c'
double
'\n' /* newline */
'\xA' /* ASCII 10 (0xA) */
12-5 12-6
12-7 12-8
2
Example Operators
#include <stdio.h>
int itsGlobal = 0;
Programmers manipulate variables
using the operators provided by the high-level language.
main()
{
int itsLocal = 1; /* local to main */ Variables and operators combine to form
printf("Global %d Local %d\n", itsGlobal, itsLocal); expressions and statements
{
int itsLocal = 2; /* local to this block */ which denote the work to be done by the program.
itsGlobal = 4; /* change global variable */
printf("Global %d Local %d\n", itsGlobal, itsLocal);
} Each operator may correspond to many
printf("Global %d Local %d\n", itsGlobal, itsLocal); machine instructions.
}
• Example: The multiply operator (*) typically requires
multiple LC-3 ADD instructions.
Output
Global 0 Local 1
Global 4 Local 2
Global 4 Local 1
12-9 12-10
Expression Statement
Any combination of variables, constants, operators, Expresses a complete unit of work
and function calls • executed in sequential order
• every expression has a type,
derived from the types of its components
(according to C typing rules) Simple statement ends with semicolon
z = x * y; /* assign product to z */
Examples: y = y + 1; /* after multiplication */
counter >= STOP ; /* null statement */
x + sqrt(y)
x & z + 3 || 9 - w-- % 6 Compound statement groups simple statements
using braces.
• syntactically equivalent to a simple statement
{ z = x * y; y = y + 1; }
12-11 12-12
3
Operators Assignment Operator
Three things to know about each operator Changes the value of a variable.
(1) Function
• what does it do? x = x + 4;
(2) Precedence
• in which order are operators combined?
1. Evaluate right-hand side.
• Example:
"a * b + c * d" is the same as "(a * b) + (c * d)" 2. Set value of left-hand side variable to result.
because multiply (*) has a higher precedence than addition (+)
(3) Associativity
• in which order are operators of the same precedence combined?
• Example:
"a - b - c" is the same as "(a - b) - c"
because add/sub associate left-to-right
12-13 12-14
12-15 12-16
4
Arithmetic Expressions Bitwise Operators
If mixed types, smaller type is "promoted" to larger. Symbol Operation Usage Precedence Assoc
x + 4.3 ~ bitwise NOT ~x 4 r-to-l
if x is int, converted to double and result is double << left shift x << y 8 l-to-r
>> right shift x >> y 8 l-to-r
12-17 12-18
12-19 12-20
5
Example How about this,
Input a character, check if it is a letter or not Input a character, check if it is NOT a letter
int c; int c;
if (c >= ‘a’ && c <= ‘z’ || c >= ‘A’ && c <= ‘Z’) if (? )
printf (“%c is a letter”, c); printf (“%c is NOT a letter”, c);
12-21 12-22
12-23 12-24
6
Using ++ and -- Practice with Precedence
Assume a=1, b=2, c=3, d=4.
x = 4;
y = x++; x = a * b + c * d / 2; /* x = 8 */
Results: x = 5, y = 4 same as:
(because x is incremented after assignment) x = (a * b) + ((c * d) / 2);
/* initialize */
inLocal = 5;
A real compiler would perform code optimizations inGlobal = 3;
that try to keep variables allocated in registers.
/* perform calculations */
Why? outLocalA = inLocal++ & ~inGlobal;
outLocalB = (inLocal + inGlobal) - (inLocal - inGlobal);
/* print results */
printf("The results are: outLocalA = %d, outLocalB = %d\n",
outLocalA, outLocalB);
}
12-27 12-28
7
Special Operator: Conditional Example
Symbol Operation Usage Precedence Assoc
?: conditional x?y:z 16 l-to-r If statement:
if (a ==5)
If x is TRUE (non-zero), result is y; b = 2;
else, result is z. else
b = 4;
Like a MUX, with x as the select signal.
Conditional expresion:
y z
b = (a == 5) ? 2 : 4;
1 0
x
12-29 12-30
(value 1) , (value 2)
12.1 -> 12.20
Example:
1. a = 2;
b = (2 , a + 1);
12-31 12-32