Big Endian vs. Little Endian Storage of Numeric Data Noah Mendelsohn Tufts University Web:

Slides:



Advertisements
Similar presentations
Manipulating Bit Fields in C Noah Mendelsohn Tufts University Web: COMP 40: Machine.
Advertisements

Copyright 2014 – Noah Mendelsohn Assemblers, Macros and The UM Macro Assembler (UMASM) Noah Mendelsohn Tufts University
INSTRUCTION SET ARCHITECTURES
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
CS2422 Assembly Language & System Programming September 19, 2006.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Boolean Algebra Discrete Mathematics and Its Applications Baojian Hua
The little language that could Remember C is a “small language”
What is an instruction set?
+ CS 325: CS Hardware and Software Organization and Architecture Integers and Arithmetic Part 4.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Pointers CSE 2451 Rong Shi.
Copyright 2013 – Noah Mendelsohn Process Memory Noah Mendelsohn Tufts University Web:
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
CENG 311 Machine Representation/Numbers
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2011.
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
Implementation of a Stored Program Computer ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides2.ppt Modification date: Oct 16,
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
C programming or Fun with pointers Tutorial #2 CPSC 261.
EPSII 59:006 Spring Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.
More on Abstract Data Types Noah Mendelsohn Tufts University Web: COMP 40: Machine.
Working With Main Memory. Why Main Memory Register space limited Used for communication.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Byte Order Mohammad Kamal. Byte order Problem with byte order Numbers vs Data Practical example for reading data Exchanging Data between different systems.
Computer Architecture EKT 422
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
Building Your Own Machine The Universal Machine (UM) Introduction Noah Mendelsohn Tufts University Web:
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Chapter 10 Instruction Sets: Characteristics and Functions Felipe Navarro Luis Gomez Collin Brown.
A Quick Look at C for C++ Programmers Noah Mendelsohn Tufts University Web: COMP.
Introduction to Computer Organization & Systems Topics: Command Line Bitwise operators COMP Spring 2014 C Part V.
Representing Negative Numbers Noah Mendelsohn Tufts University Web: COMP 40: Machine.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Data Types Always data types will decide which type of information we are storing into variables In C programming language we are having 3 types of basic.
More on Abstract Data Types Noah Mendelsohn and Mark Sheldon Tufts University Web:
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
The Universal Machine (UM) Implementing the UM Noah Mendelsohn Tufts University Web:
Byte Addressability Bytes are always 8 bits Word length typically ranges from 16 to 64 bits. Memory location assignments refer to successive byte locations.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2014.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
The Machine Model Memory
Process Memory COMP 40: Machine Structure and
ECE Application Programming
Memory, Data, & Addressing II CSE 351 Autumn 2017
EPSII 59:006 Spring 2004.
COMP3221: Microprocessors and Embedded Systems
Basic notes on pointers in C
Computer Programming Machine and Assembly.
Bits and Bytes Topics Representing information as bits
September 5 Fang-Yi’s Office Hours Friday 10am  12pm (and another TBD) Programming Language Issues (JAVA vs. C, Pointers vs. Arrays) Representations Instructions.
Little Endian vs. Big Endian (Intel vs. Motorola)
Bits and Bytes Topics Representing information as bits
Lecture 2 SCOPE – Local and Global variables
Comp Org & Assembly Lang
Bits, Bytes, and Integers Part 2 3rd Lectures
Comp Org & Assembly Lang
Variables and Computer Memory
Chapter 10 Instruction Sets: Characteristics and Functions
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

Big Endian vs. Little Endian Storage of Numeric Data Noah Mendelsohn Tufts University Web: COMP 40: Machine Structure and Assembly Language Programming (Spring 2014)

© 2010 Noah Mendelsohn Goals for this presentation  Explore two different conventions for storing numbers in computer memory  Learn the specifics of “Big-endian” and “Little-endian” representations  Focus on “little-endian” – used by our AMD 64 computers  Note: none of this affects the storage of characters or character strings! Here, we are discussing only numeric types. 2

© 2010 Noah Mendelsohn The Problem

© 2010 Noah Mendelsohn What’s the issue?  We usually think of an integer variable as a single value: int myint = 0x1A2B3C4E;  If we store it in memory, that takes 4 bytes, each of which is addressible…which byte of the int is stored first? 4.

© 2010 Noah Mendelsohn What’s the issue?  We usually think of an integer variable as a single value: int myint = 0x1A2B3C4E;  If we store it in memory, that takes 4 bytes, each of which is addressible…which byte of the int is stored first? 5 1A2B3C4E 3C2B1A Big Endian Little Endian

© 2010 Noah Mendelsohn What’s the issue?  We usually think of an integer variable as a single value: int myint = 0x1A2B3C4E;  If we store it in memory, that takes 4 bytes, each of which is addressible…which byte of the int is stored first? 6 1A2B3C4E 3C2B1A Big Endian Little Endian

© 2010 Noah Mendelsohn What’s the issue?  We usually think of an integer variable as a single value: int myint = 0x1A2B3C4E;  If we store it in memory, that takes 4 bytes, each of which is addressible…which byte of the int is stored first? 7 1A2B3C4E 3C2B1A Big Endian Little Endian The choice depends on the model of computer you are using! Our AMD64 machines are little endian!

© 2010 Noah Mendelsohn Pointing to integers in memory  We usually think of an integer variable as a single value: int myint = 0x1A2B3C4E; int *ip = &myint;  If we store it in memory, that takes 4 bytes, each of which is addressible…which byte of the int is stored first? 8 1A2B3C4E 3C2B1A Big Endian Little Endian Pointer is always address of first byte.

© 2010 Noah Mendelsohn Example: positive number  We usually think of an integer variable as a single value: int myint = 258; Big Endian Little Endian REMEMBER: Our AMD 64 machines are little endian!

© 2010 Noah Mendelsohn Example: negative number  We usually think of an integer variable as a single value: int myint = (-2); 10 FF FE Big Endian FEFF Little Endian

© 2010 Noah Mendelsohn Can we ever observe the difference? 11 int main(int argc, char *argv[]) { (void) argc; (void) argv; int pos = 258; int neg = (-2); float float12 = 12.0; float floatneg12 = (-12.0); printf("The bytes in memory for signed integer %d are ", pos); printbytes(&pos, sizeof(pos)); printf("\n"); printf("The bytes in memory for signed integer %d are ", neg); printbytes(&neg, sizeof(neg)); printf("\n"); printf("The bytes in memory for float %f are ", float12); printbytes(&float12, sizeof(float12)); printf("\n"); printf("The bytes in memory for float %f are ", floatneg12); printbytes(&floatneg12, sizeof(floatneg12)); printf("\n"); } /* * Print bytes in memory in hex */ void printbytes(void *p, unsigned int len) { unsigned int i; unsigned char *cp = (unsigned char *)p; for (i=0; i<len; i++) { printf("%02X", *cp++); } RUN THIS PROGRAM ON OUR MACHINES!! The bytes in memory for signed integer 258 are The bytes in memory for signed integer -2 are FEFFFFFF The bytes in memory for float are The bytes in memory for float are C1 Output:

© 2010 Noah Mendelsohn Do we care?  Mostly, we don’t worry about it…variables generally work as you would expect  When we store data in memory, the “endianness” matters  Times you care most: –When writing numeric variables or arrays from memory to files –When writing numeric variables or arrays from memory to a network –In these cases, you and the reader must agree on byte order  Note that HW4 specifies the “endianness” of the output file you must produce! 12

© 2010 Noah Mendelsohn How did this happen?  Both ways “work”  Many people feel big-endian is most natural, but…  There are some advantages for little-endian: –Regardless of int, long,etc, you always consistently address the low order byte with pointers. –A simple addition circuit can work from low addresses to high, doing addition or subtraction in the natural way. 13