X86 Programming Memory Accessing Modes, Characters, and Strings Computer Architecture.

Slides:



Advertisements
Similar presentations
INSTRUCTION SET ARCHITECTURES
Advertisements

There are two types of addressing schemes:
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
MIPS Assembly Language Programming
Microprocessors General Features To be Examined For Each Chip Jan 24 th, 2002.
Department of Computer Science and Software Engineering
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
COMP3221: Microprocessors and Embedded Systems--Lecture 4 1 COMP3221: Microprocessors and Embedded Systems Lecture 4: Number Systems (II)
Microprocessor Systems Design I
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#8) By Dr. Syed Noman.
Lecture 18 Last Lecture Today’s Topic Instruction formats
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
Some material taken from Assembly Language for x86 Processors by Kip Irvine © Pearson Education, 2010 Slides revised 2/2/2014 by Patrick Kelley.
MOV,MOVM,COP,FLL,BSL,BSR, FRD,TOD
CPS120: Introduction to Computer Science
6.828: PC hardware and x86 Frans Kaashoek
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Input & Output: Console
Lecture 11 Last notes on interrupts and exam review Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Machine Instruction Characteristics
1/2002JNM1 Positional Notation (Hex Digits). 1/2002JNM2 Problem The 8086 has a 20-bit address bus. Therefore, it can access 1,048,576 bytes of memory.
Number Systems Spring Semester 2013Programming and Data Structure1.
Implementation of a Stored Program Computer ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides2.ppt Modification date: Oct 16,
10-Sep Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Sept Representing Information in Computers:  numbers: counting numbers,
INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Module 3 Instruction Set Architecture (ISA): ISA Level Elements of Instructions Instructions Types Number of Addresses Registers Types of Operands.
4-1 Chapter 4 - The Instruction Set Architecture Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
CET 3510 Microcomputer Systems Tech. Lecture 2 Professor: Dr. José M. Reyes Álamo.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Computer Architecture and Organization
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Chapter 10 Instruction Sets: Characteristics and Functions Felipe Navarro Luis Gomez Collin Brown.
MECH1500 Chapter 3.
Functions/Methods in Assembly
What is a program? A sequence of steps
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Chapter 8 String Operations. 8.1 Using String Instructions.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Practical Session 5.
Computer Architecture and Assembly Language
Programming and Data Structure
Assembly language.
Credits and Disclaimers
ECE 3430 – Intro to Microcomputer Systems
Assembly Lab 3.
Data Transfers, Addressing, and Arithmetic
Number Representation
Microprocessor Systems Design I
Homework Reading Labs PAL, pp
Microprocessor Systems Design I
Additional Assembly Programming Concepts
Aaron Miller David Cohen Spring 2011
Computer Architecture
Computer Organization and Assembly Language (COAL)
COMP3221: Microprocessors and Embedded Systems
BIC 10503: COMPUTER ARCHITECTURE
Data-Related Operators and Directives
ECEG-3202 Computer Architecture and Organization
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
ECEG-3202 Computer Architecture and Organization
Machine-Level Programming II: Basics Comp 21000: Introduction to Computer Organization & Systems Instructor: John Barr * Modified slides from the book.
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Chapter 10 Instruction Sets: Characteristics and Functions
Presentation transcript:

x86 Programming Memory Accessing Modes, Characters, and Strings Computer Architecture

Multi byte storage Multi-byte data types include: –word/short (2 bytes) –int (4 bytes) –long or quad (8 bytes) Conceptual representation –Most significant byte (MSB) is left most byte –Least significant byte (LSB) is right most byte –Example: Number: 0xaabb MSB: 0xaa LSB: 0xbb In memory representation (applicable only to multi byte storage) –Big Endian MSB is stored at the lower memory address –Little Endian MSB is stored at the higher memory address

Big vs. Little Endian Consider the integer: 0x11aa22bb Big Endian Storage Little Endian Storage (x86 architecture) 0x10000x10010x10020x1003 0x110xaa0x220xbb 0x10000x10010x10020x1003 0xbb0x220xaa0x11 Memory Address

Characters Characters are simply represented using an unsigned 8-bit (byte) numbers – In memory as well as in instructions. –The number is interpreted and displayed as characters for Input-Output (I/O) purposes only! –The mapping from byte values to character (as displayed on screen) is based on the American Standard Code for Information Interchange (ASCII) It is used all over the world by all I/O devices –Like: Monitors, keyboards, etc.

Standard ASCII Codes Here is a short table illustrating standard ASCII codes that are frequently used: Range of ASCII CodesRange of Characters to ‘0’ to ‘9’ to ‘A’ to ‘Z’ to ‘a’ to ‘z’

Characters in assembly Example assembly code with 5 characters –Note that the characters stored at consecutive memory addresses! It is guaranteed by the assembler! /* Assembly program involving characters */.text /* Instructions */.data char1:.byte 72 /* ASCII code for ‘H’ */ char2:.byte 101 /* ASCII code for ‘e’ */ char3:.byte 108 /* ASCII code for ‘l’ */ char4:.byte 108 /* ASCII code for ‘l’ */ char5:.byte 111 /* ASCII code for ‘o’ */

For the Java programmer… Assembler permits direct representation of characters –It converts characters to ASCII codes /* Assembly program involving characters */.text /* Instructions */.data char1:.byte ’ H’ /* Assembler converts the */ char2:.byte ’ e’ /* characters to ASCII */ char3:.byte ’ l’ char4:.byte ’ l’ char5:.byte ’ o ’

Memory organization Bytes declared consecutively in the assembly source are stored at consecutive memory locations –Assume that the assembler places char1 (‘H’) at address 0x20, then other characters have the following memory addresses: 0x20 H 0x21 e 0x22 l 0x23 l 0x24 o Addresses

Working with characters All characters (including other symbols) have 2 unique values associated with them –The address in memory Accessed by prefixing the symbol with a $ (dollar) sign The memory address is always 32-bits (4 bytes) on 32- bit x86 processors –It is 64-bits wide on 64-bit x86 processors. –The value contained in the memory location Accessed without any prefixes to the symbol. The bytes read depends on the type of the symbol –1 byte for byte, 4 bytes for int etc. This is exactly how we have been doing it so far.

Cross Check Given the following memory layout and symbol table what are the values of: –$letter: 0x20 –Yellow: ‘e’ –$k: 0x22 –e: ‘o’ 0x20 H 0x21 e 0x22 l 0x23 l 0x24 o Address letter 0x20 SymbolAddress Yellow 0x21 k0x22 e 0x24 Addresses of symbols (expressions with a $ sign) are obtained from the symbol table while values of symbols (expressions without $ sign) are obtained from the memory layout shown below.

Example assembly /* Example use of characters */.text movb char1, %al /* al = ASCII(‘H’) */ addb $1, %al /* al = ASCII(‘I’) */ movb %al, char1 /* char1 = (‘I’) */ movl $char1, %ebx /* ebx = addressOf(char1) */.data char1:.byte ‘H’

What’s the use of addresses? Why bother loading addresses into registers? –x86 permits indirect memory access and manipulation using addresses stored in registers! –A variety of mechanisms are supported by x86 processors for generating the final memory address for retrieving data The variety of mechanism is collectively called memory Addressing Modes

Addressing Modes x86 supports the following addressing modes 1.Register mode 2.Immediate mode 3.Direct mode 4.Register direct mode 5.Base displacement mode 6.Base-index scaled mode

Register mode  Instructions involving only registers  This is the simplest and fastest mechanism  Data is loaded and stored to registers.  In this mode, the processor does not access RAM..text movb %al, %ah /* ah = al */ addl %eax, %ebx /* ebx += eax */ mull %ebx /* eax *= ebx */

Immediate mode  Instructions involving registers & constants  This mode is used to load constant values into registers  The constant value to be loaded is encoded as a part of the instruction.  Consequently, there is no real memory access.text movb $5, %ah /* ah = 5 */ addl $-35, %ebx /* ebx += -35 */

Direct Mode Standard mode used with symbols –Address to load/store data is part of instruction Involves 1 memory access using the address Number of bytes loaded depends on type Symbols are used to represent addresses –Source/Destination has to be a register!.text movb char1, %ah /* ah = ‘H’ */ addl %eax, i1 /* i1 += eax */.data char1:.byte ‘H’ i1:.int 100

Register direct mode Address for memory references are obtained from a register. –The address needs to be loaded into a register. Addresses can be manipulated as a regular number!.text /* eax = addressOf(char1) */ movl $char1, %eax movb (%eax), %bl /* bl = ‘H’ */ inc %eax /* eax++ */ movb %bl, (%eax) /* char2 = char1 */.data char1:.byte ‘H’ char2:.byte ‘e’

Register direct mode (Contd.) Register direct mode is most frequently used! –It is analogous to accessing using references in Java –Note that one of the operands in register direct mode has to be a register –Pay attention to the following syntax $symbol: To obtain address of symbol –Address is always 32-bits! (%register): Data stored at the memory address contained in register. –The number of bytes read from the given memory location depends on the instruction.

Base Displacement Mode Constant offset from a given address stored in a register –Used to access parameters to a method We will see the use for this mode in the near future..text /* eax = addressOf(char1) */ movl $char1, %eax movb 1(%eax), %bl /* bl = char2 */ inc %eax movb %bl, -1(%eax) /* char1 = char2 */.data char1:.byte ‘H’ char2:.byte ‘e’ Displacement value is constant. The base value is contained in registers!

Base-Index scaled Mode Most complex form of memory referencing Involves a displacement constant A base register An index register A scale factor (must be 0, 1, 2, 4, or 8) –Final address for accessing memory is computed as: address = base_register + (index_register * scale_factor) + displacement_constant

Base-Index scaled Mode Examples of this complex mode is shown below:.text /* eax = addressOf(char1) */ movl $char1, %eax movl $0, %ebx movb 1(%eax, %ebx, 4), %bl /*bl=char2*/ inc %eax movl $1, %ebx movb %bl, -1(%eax, %ebx, 0).data char1:.byte ‘H’ char2:.byte ‘e’ Address = %eax + (%ebx * 4) + 1 = %eax + (0 * 4) + 1 = %eax + 1 Address = %eax + (%ebx * 0) - 1 = %eax + (1 * 0) - 1 = %eax - 1

LEA Instruction X86 architecture provides a special instruction called LEA (Load Effective Address) –This instruction loads the effective address resulting from applying various memory access modes into a given register. –Examples: LEA -1(%eax, %ebx, 0), %edi LEA (%eax, %ebx), %edi LEA -5(%eax), %edi

LEA Example (Contd.) Here is an example of the LEA instruction.text /* eax = addressOf(char1) */ movl $char1, %eax movl $0, %ebx lea 1(%eax, %ebx, 2), %edi /*edi = address of char2*/ movb $’h’, (%edi) /* change ‘e’ to ‘h’*/.data char1:.byte ‘H’ char2:.byte ‘e’

Strings Strings are simply represented as a sequence (or array) of characters in memory –Each character is stored at a consecutive memory address! –Every string is terminated by ASCII value 0 Represented as ‘\0’ in assembly source

Declaring Strings in Assembly Strings are defined using the.string directive.text /* Instructions go here */.data msg1:.string “Hello\n” msg2:.string “World!\n”

Memory representation Given the previous example, the strings (msg1 and msg2) are stored in memory as shown below: Hello World \n\0 !\n\ A 2B2C2D 2E msg1=20 msg2=27.text /* Instructions go here */.data msg1:.string “Hello\n” msg2:.string “World!\n”

Displaying Strings Strings or characters can be displayed on standard output (analogous to System.out) using System call: –Set eax to 4 To write characters to a file (stream) Changing eax to 3 will cause reading characters instead! –Set ebx to 1 Destination steam is standard output You may set ebx to 2 for standard error If ebx is 0 it indicates standard input (you can write to it!) –Set ecx to address of message to display –Set number of characters to display in edx –Call int 0x80

Complete Example /* Console output example */ text.global _start _start: mov $4, %eax /* System call to write to a file handle */ mov $1, %ebx /* File handle=1 implies standard output */ mov $msg, %ecx /* Address of message to be displayed */ mov $14, %edx /* Number of bytes to be displayed */ int $0x80 /* Call OS to display the characters. */ mov $1,%eax /* The system call for exit (sys_exit) */ mov $0,%ebx /* Exit with return code of 0 (no error) */ int $0x80.data /* The data to be displayed */ msg:.string "Hello!\nWorld!\n" Calculated value by hand! Can be cumbersome for large strings.

Rewritten using Macro! /* Console output example */ text.global _start _start: mov $4, %eax /* System call to write to a file handle */ mov $1, %ebx /* File handle=1 implies standard output */ mov $msg, %ecx /* Address of message to be displayed */ mov $len, %edx /* Number of bytes to be displayed */ int $0x80 /* Call OS to display the characters. */ mov $1,%eax /* The system call for exit (sys_exit) */ mov $0,%ebx /* Exit with return code of 0 (no error) */ int $0x80.data /* The data to be displayed */ msg:.string "Hello!\nWorld!\n“.equ len,. - msg Compute a assembler constant len by subtracting address of msg from current address, represented by special symbol (dot). Every use of $msg is replaced with the resulting constant value.

Compute string length The previous examples use fixed length strings –For strings that change values or change lengths, the string length must be computed using suitable assembly code. –The corresponding Java source is shown below: public static int length(char[] str) { int i; for(i = 0; (str[i] != ‘\0’); i++); return i; }

Compute string length _length: /* Let eax correspond to i */ movl $0, %eax /* eax = 0 * / /* Let ebx correspond to str */ movl $str, %ebx /* ebx = address(str) */ loop: cmpb $0, (%ebx, %eax) /* str[i] != ‘\0’ */ je done /* We have hit the ‘\0’ in string */ inc %eax /* i++ */ jmp loop /* Continue the loop */ done: Base register = ebx Offset register = eax Displacement (implicit)= 0 Scale value (implicit) = 1