1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Introduction to computing systems (2/e): Chapter 12 - Yale N. Patt, Sanjay J. Patel

32 46 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 32
Dung lượng 391,21 KB

Nội dung

Chapter 12 - Variables and operators. This chapter presents the following content: Basic C Elements, data types, variable names, literals, scope: global and local, operators, statement, assignment operator,...and other contents.

Chapter 12 Variables and Operators Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Basic C Elements Variables • named, typed data items Operators • predefined actions performed on data items • combined with variables to form expressions, statements Rules and usage Implementation using LC-2 12­2 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Data Types C has three basic data types int double char integer (at least 16 bits) floating point (at least 32 bits) character (at least bits) Exact size can vary, depending on processor • int is supposed to be "natural" integer size; for LC-2, that's 16 bits 32 bits for most modern processors 12­3 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Variable Names Any combination of letters, numbers, and underscore (_) Case matters • "sum" is different than "Sum" Cannot begin with a number • usually, variables beginning with underscore are used only in special library routines Only first 31 characters are used 12­4 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Examples Legal i wordsPerSecond same identifier words_per_second _green aReally_longName_moreThan31chars aReally_longName_moreThan31characters Illegal 10sdigit ten'sdigit done? double reserved keyword 12­5 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Literals Integer 123 /* -123 0x123 /* Floating point 6.023 6.023e23 5E12 Character 'c' '\n' /* '\xA' /* decimal */ hexadecimal */ /* 6.023 x 1023 */ /* 5.0 x 1012 */ newline */ ASCII 10 (0xA) */ 12­6 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Scope: Global and Local Where is the variable accessible? Global: accessed anywhere in program Local: only accessible in a particular region Compiler infers scope from where variable is declared • programmer doesn't have to explicitly state Variable is local to the block in which it is declared • block defined by open and closed braces { } • can access variable declared in any "containing" block Global variable is declared outside all blocks 12­7 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Example #include int itsGlobal = 0; main() { int itsLocal = 1; /* local to main */ printf("Global %d Local %d\n", itsGlobal, itsLocal); { int itsLocal = 2; /* local to this block */ itsGlobal = 4; /* change global variable */ printf("Global %d Local %d\n", itsGlobal, itsLocal); } printf("Global %d Local %d\n", itsGlobal, itsLocal); } Output Global Local Global Local Global Local 12­8 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Symbol Table Like assembler, compiler needs to know information associated with identifiers • in assembler, all identifiers were labels and information is address Compiler keeps more information Name (identifier) Name Type counter startPoint Location in memory itsGlobal Scope Type Offset Scope int int int main main global 12­9 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Allocating Space for Variables Global data section 0x0000 • All global variables stored here (actually all static variables) • R5 points to beginning Run-time stack • Used for local variables • R6 points to storage area at top of stack • New storage area for each block (goes away when block exited) Offset = distance from beginning of storage area 0xFFFF • Global: LDR R1, R5, #4 • Local: LDR R2, R6, #6 instructions global data PC R5 stack R6 12­10 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Arithmetic Operators Symbol * / % + - Operation multiply divide modulo addition subtraction Usage x * y x / y x % y x + y x - y Precedence Assoc l-to-r l-to-r l-to-r l-to-r l-to-r All associate left to right * / % have higher precedence than + - 12­18 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Arithmetic Expressions If mixed types, smaller type is "promoted" to larger x + 4.3 if x is int, converted to double and result is double Integer division fraction is dropped x / if x is int and x=5, result is (not 1.666666 ) Modulo result is remainder x % if x is int and x=5, result is 12­19 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Practice with Precedence Assume a=1, b=2, c=3, d=4 x = a * b + c * d / 2; /* x = */ same as: x = (a * b) + ((c * d) / 2); For long or confusing expressions, use parentheses, because reader might not have memorized precedence table Note: Assignment operator has lowest precedence, so all the arithmetic operations on the right-hand side are evaluated first 12­20 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Bitwise Operators Symbol ~ > & ^ | Operation bitwise NOT left shift right shift bitwise AND bitwise XOR bitwise OR Usage ~x x > y x & y x ^ y x | y Precedence Assoc r-to-l l-to-r l-to-r 11 l-to-r 12 l-to-r 13 l-to-r Operate on variables bit-by-bit • Like LC-2 AND and NOT instructions Shift operations are logical (not arithmetic) Operate on values neither operand is changed 12­21 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Logical Operators Symbol ! && || Operation logical NOT logical AND logical OR Usage !x x && y x || y Precedence Assoc r-to-l 14 l-to-r 15 l-to-r Treats entire variable (or value) as TRUE (non-zero) or FALSE (zero) Result is (TRUE) or (FALSE) 12­22 Copyright © The McGraw-Hill Companies, Inc Permission required for reproduction or display Relational Operators Symbol Operation > greater than >= greater than or equal < less than y l-to-r x >= y l-to-r x < y l-to-r x

Ngày đăng: 30/01/2020, 00:53

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN