Byte Ordering. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Similar documents
Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Bits and Bytes January 13, 2005

Bits, Bytes, and Integers Part 2

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

Lecture 5-6: Bits, Bytes, and Integers

Topics of this Slideset. CS429: Computer Organization and Architecture. It s Bits All the Way Down. Why Binary? Why Not Decimal?

CS429: Computer Organization and Architecture

Hardware: Logical View

Machine-level Representation of Programs

CSE351: Memory, Data, & Addressing I

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

CS140 Lecture 08: Data Representation: Bits and Ints. John Magee 13 February 2017

Bits and Bytes: Data Presentation Mohamed Zahran (aka Z)

Bits, Bytes, and Integers August 26, 2009

Bits, Bytes, and Integers

Course overview. Computer Organization and Assembly Languages Yung-Yu Chuang 2006/09/18. with slides by Kip Irvine

Bits, Bytes and Integers

Computer Systems CEN591(502) Fall 2011

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

Representing Integers. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Instruction Set Architecture

Representation of Information

Data Representa5on. CSC 2400: Computer Systems. What kinds of data do we need to represent?

Data Representa+on in Memory

Representing and Manipulating Integers Part I

CS , Fall 2001 Exam 1

CS429: Computer Organization and Architecture

Outline. x86 Architecture

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #3: Of Integers and Endians (pt. 2)

Do not turn the page until 5:10.

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

Foundations of Computer Systems

Computer Systems Programming. Practice Midterm. Name:

Pointers (1A) Young Won Lim 2/6/18

Pointers (1A) Young Won Lim 2/10/18

Lecture 4: Instruction Set Architecture

Reverse Engineering II: The Basics

Course overview. Computer Organization and Assembly Languages Yung-Yu Chuang 2007/09/17. with slides by Kip Irvine

Memory, Data, & Addressing I

Jin-Soo Kim Systems Software & Architecture Lab. Seoul National University. Integers. Spring 2019

17. Instruction Sets: Characteristics and Functions

Full Name: CISC 360, Fall 2008 Example of Exam

Machine-level Representation of Programs. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Static Analysis I PAOLO PALUMBO, F-SECURE CORPORATION

M1 Computers and Data

Type (1A) Young Won Lim 2/17/18

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Data Representation and Storage. Some definitions (in C)

Practical Malware Analysis

CS , Fall 2004 Exam 1

Computer Organization: A Programmer's Perspective

Assembly IV: Complex Data Types. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS , Spring 2004 Exam 1

Survey. Motivation 29.5 / 40 class is required

Computer System and programming in C

CS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

ECE2049 HW #1-- C programming and Binary Number Representations (DUE Friday 8/31/2018 At the BEGINNING of class)

Do not turn the page until 5:10.

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Representing and Manipulating Floating Points

CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Architecture I. Computer Systems Laboratory Sungkyunkwan University

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

CS , Fall 2002 Exam 1

Memory Organization and Addressing

Data Types. Data Types. Integer Types. Signed Integers

Pointers (1A) Young Won Lim 1/22/18

Instruction Sets: Characteristics and Functions

Assembly IV: Complex Data Types. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Pointers (1A) Young Won Lim 1/14/18

The type of all data used in a C (or C++) program must be specified

Representing and Manipulating Floating Points

Page 1. Structure of von Nuemann machine. Instruction Set - the type of Instructions

Representing Integers

These are reserved words of the C language. For example int, float, if, else, for, while etc.

CS367 Test 1 Review Guide

ECE2049 HW #1-- C programming and Binary Number Representations (DUE 1/19/2018 At the BEGINNING of class)

Representing and Manipulating Floating Points. Computer Systems Laboratory Sungkyunkwan University

Beginning C Programming for Engineers

The type of all data used in a C++ program must be specified

T Reverse Engineering Malware: Static Analysis I

MIPS Instruction Set Architecture (1)

ARM Cortex-M4 Programming Model

Data Storage and Query Answering. Data Storage and Disk Structure (4)

ECE2049 HW #1-- C programming and Binary Number Representations (DUE 9/1/2017 At the BEGINNING of class)

Chapter 1. Computer Abstractions and Technology. Lesson 3: Understanding Performance

Computer Systems Laboratory Sungkyunkwan University

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

CSCI 402: Computer Architectures

Arithmetic and Bitwise Operations on Binary Data

CS C Primer. Tyler Szepesi. January 16, 2013

Distributed Systems 8. Remote Procedure Calls

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Chapter 2A Instructions: Language of the Computer

Transcription:

Byte Ordering Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

Memory Model Physical memory DRAM chips can read/write 4, 8, 16 bits DRAM modules can read/write 64 bits Programmer s view of memory Conceptually, a very large array of bytes Stored-program computers: keeps program codes and data in memory Running programs share the physical memory OS handles memory allocation and management... 2

Machine Words Each computer has a word size Nominal size of integer-valued data Including addresses (= pointer size) Until recently, most machines used 32-bit (4-byte) words Limits addresses to 4 GB Becoming too small for memory-intensive applications Increasingly, machines have 64-bit (8-byte) word size Potential address space 18.4 х 10 18 bytes (18 EB) x86-64 machines support 48-bit addresses: 256 TB Machines support multiple data formats Fractions or multiples of word size Always integral number of bytes 3

Word-level Memory Access Addresses specify byte locations Address of first byte in word Addresses of successive words differ by 4 (32-bit) or 8 (64-bit) Usually, addresses should be aligned to the word boundary 32-bit Words Addr = Addr = 04 Addr = 08 Addr = 12 64-bit Words Addr = Addr = 08 Bytes Addr. 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 4

Data Types in C C Data Type Typical 32-bit Typical 64-bit x86-64 char 1 1 1 short 2 2 2 int 4 4 4 long 4 8 8 long long 8 8 8 float 4 4 4 double 8 8 8 long double - - 10/16 pointer 4 8 8 5

Byte Ordering How are the bytes within a multi-byte word ordered in memory? Conventions Big endian: Sun, PowerPC Mac, Internet Little endian: Intel x86, ARM running Android & ios Note: Alpha and PowerPC can run in either mode, with the byte ordering convention determined when the chip is powered up Problem when the binary data is communicated over a network between different machines 6

Big vs. Little Endian Big endian Least significant byte has highest address Little endian Least significant byte has lowest address Register Register Memory Memory Big endian Little endian 7

Example 1 Disassembly Text representation of binary machine code Generated by program that reads the machine code Example fragment Address Instruction Code Assembly Rendition 8048365: 5b pop %ebx 8048366: 81 c3 ab 12 add $0x12ab,%ebx 804836c: 83 bb 28 cmpl $0x0,0x28(%ebx) Deciphering numbers: Value: 0x12ab Pad to 32 bits: 0x12ab Split into bytes: 12 ab Reverse: ab 12 8

Example 2 What is the output of this program? Solaris/SPARC:? Linux/x86:? #include <stdio.h> union { int i; unsigned char c[4]; } u; int main () { u.i = 0x12345678; printf ( %x %x %x %x\n, u.c[0], u.c[1], u.c[2], u.c[3]); } 9

Representing Integers int A = 15213; int B = -15213; long int C = 15213; IA32, x86-64 A 6D 3B IA32, x86-64 B 93 C4 Sun A 3B 6D Sun B C4 93 Decimal: 15213 Binary: 11 1011 0110 1101 Hex: 3 B 6 D IA32 C 6D 3B x86-64 C 6D 3B Two s complement representation Sun C 3B 6D 10

Representing Pointers int B = -15213; int *P = &B; Sun P EF FB 2C IA32 P D4 F8 BF x86-64 P 0C 89 EC 7F Different compilers & machines assign different locations to objects Even get different results each time run program 11

Representing Strings Strings in C Represented by array of characters Each character encoded in ASCII format Standard 7-bit encoding of character set Character 0 has code 0x30 Digit i has code 0x30 + I String should be null-terminated Final character = 0x Compatibility Byte ordering not an issue char S[6] = "15213"; Linux/Alpha S Sun S 31 35 32 31 33 31 35 32 31 33 12

Summary It s all about bits & bytes Numbers, programs, text, Different machines follow different conventions Word size Byte ordering Representations (integer, floating-point) When programming, be aware of Type casting & mixed signed/unsigned expressions Overflow Error propagation Byte ordering 13