Byte Ordering. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Similar documents
Byte Ordering. Jin-Soo Kim 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, Bytes, and Integers Part 2

Bits and Bytes January 13, 2005

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

CSE351: Memory, Data, & Addressing I

Machine-level Representation of Programs

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

Representing Integers

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

Bits, Bytes, and Integers August 26, 2009

Bits, Bytes, and Integers

Bits, Bytes and Integers

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

Computer Systems CEN591(502) Fall 2011

MIPS Instruction Set Architecture (1)

Outline. x86 Architecture

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

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?

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

Instruction Set Architecture

Representation of Information

Assembly IV: Complex Data Types

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

Data Representa+on in Memory

CS , Fall 2001 Exam 1

Representing and Manipulating Floating Points

CS429: Computer Organization and Architecture

Manipulating Integers

Foundations of Computer Systems

17. Instruction Sets: Characteristics and Functions

Lecture 4: Instruction Set Architecture

Digital Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Do not turn the page until 5:10.

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

Computer Systems Programming. Practice Midterm. Name:

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

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

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

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

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

Reverse Engineering II: The Basics

Memory, Data, & Addressing I

Full Name: CISC 360, Fall 2008 Example of Exam

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

CS , Spring 2004 Exam 1

Static Analysis I PAOLO PALUMBO, F-SECURE CORPORATION

M1 Computers and Data

Virtual Memory. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Data Type Fall 2014 Jinkyu Jeong

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

CSCI 402: Computer Architectures

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.

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

Advanced Processor Architecture

SSE3052: Embedded Systems Practice

T Reverse Engineering Malware: Static Analysis I

CS367 Test 1 Review Guide

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

Memory Organization and Addressing

Processor Architecture

CS , Fall 2002 Exam 1

Data Types. Data Types. Integer Types. Signed Integers

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

Instruction Sets: Characteristics and Functions

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

ARM Cortex-M4 Programming Model

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

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

Page Tables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Memory, Data, & Addressing II CSE 351 Spring

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

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

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

Time (self-scheduled): Location Schedule Your Exam: What to bring:

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

Chapter 2A Instructions: Language of the Computer

File System Implementation

Beginning C Programming for Engineers

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

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

ECE 154A Introduction to. Fall 2012

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

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

Transcription:

Byte Ordering Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@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... SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 2 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 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 3 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 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 4 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 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 5 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 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 6 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 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 7 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 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 8 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]); } SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 9 9

Representing Integers 1 st byte 2 nd byte 3 rd byte 4 th byte 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 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 1010

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 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 1111

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 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 1212

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 SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu) 1313