Memory Basics and Assembly Programs by Nicole Tobias
|
|
- Phoebe Clark
- 7 years ago
- Views:
Transcription
1 Memory Basics and Assembly Programs by Nicole Tobias Adapted from text: LINUX Assembly Programming by Neveln Appendix C from Computer Organization and Design by Patterson and Hennessey
2 Basics of Memory Layout In all computers, main memory is organized as a rectangle of bits. Ex. A tiny six bit by four bit memory: Address Each row is called a word Words Transfers of data are done a word at a time, or in multiples of words
3 Memory Basics The number of bits in a word is called word size Last example, would be said to have four-bit words Each word in this example memory holds a number representable in four binary digits. For example, a processor requesting the word located at the address 3 would be given the number 8. When designing a chess playing computer, for example (again), it would be a good idea to use 64-bit words One bit for each location on the chess board.
4 Words Words in memory are numbered consecutively, starting from zero. These numbers are called addresses A computer with n words will have addresses from 0 through n-1. The total capacity of a computer memory is the number of words in the memory times the word size (length times width) Note: Memory capacity is often measured in bytes instead of bits.
5 Basics and Storing Words in Memory Each address stores one element of the memory "array". typically one byte. There are some memory configurations where each address stores something besides a byte. For example, you might store a nybble or a bit. I will sometimes say that memory is byteaddressable. This is just a fancy way of saying that each address stores one byte. If I say memory is nybble-addressable, that means each memory address stores one nybble.
6 Storing Words in Memory We ll defined a word to mean 32 bits. This is the same as 4 bytes. Why would we care about 32 bits? Integers, single-precision floating point numbers, and MIPS instructions are all 32 bits long. How can we store these values into memory? After all, each memory address can store a single byte, not 4 bytes. The answer is simple.
7 Storing Words in Memory We split the 32 bit quantity into 4 bytes. For example, suppose we have a 32 bit quantity, written as 90AB12CD 16, which is hexadecimal. Since each hex digit is 4 bits, we need 8 hex digits to represent the 32 bit value. So, the 4 bytes are: 90, AB, 12, CD where each byte requires 2 hex digits. It turns out there are two ways to store this in memory.
8 Little Endian vs Big Endian For example, suppose we have a 32 bit quantity, written as 90AB12CD 16 In little endian, you store the least significant byte in the smallest address. In big endian, you store the most significant byte in the smallest address. Address Value AB CD Address Value 1003 CD AB
9 Which way makes sense? Different ISAs use different endianness. While one way may seem more natural to you (most people think big-endian is more natural), there is justification for either one. For example, IBMs(?) are little endian, while Motorolas and Suns are big endian. MIPS processors allowed you to select a configuration where it would be big or little endian.
10 Why is Endianness so important? Suppose you are storing int values to a file, then you send the file to a machine which uses the opposite endianness and read in the value. You'll run into problems because of endianness. You'll read in reversed values that won't make sense.
11 Why is Endianness so important? Endianness is also a big issue when sending numbers over the network. Again, if you send a value from a machine of one endianness to a machine of the opposite endianness, you'll have problems. This is even worse over the network, because you might not be able to determine the endianness of the machine that sent you the data.
12 Heaps and Stacks
13 Memory Layout (Big Picture)
14 The Stack A visual representation of the way memory is stored Works from top down Uses: 1. Temporary storage of data 2. Parameter passing 3. Holding subprogram temporary variables Operations: push and pop
15 Assembly BoilerPlate [main: push ebp mov ebp, esp push ebx push esi push edi ;;; Your code here ] pop edi pop esi pop ebx mov esp, ebp pop ebp ret
16 Linux Demo Accessing a LinuxVM Creating your first program VIM Compiling your first program Running your first program
17 Assembly Language Process: Compile-Assemble-Link to machine lang Machine Language is platform dependent Assembly language format: mnemonic-operand(s) Mnemonic is the instruction Operand(s) are the item(s) manipulated
18 Instructions A few basic instructions that we will be using: mov add jmp
19 Instructions (cont.) mov Format: mov destination, source Data is source is copied to destination mov is the mnemonic destination and source are the operands add Format: add destination, source1, source2 add source1 and source2 and place the results in destination
20 Instructions (cont.) jmp Format: jmp label continue execution of the program at the line named label
21 Memory in NASM Bit smallest data item Nybble 4 bits Byte 8 bits or 2 nybbles Word 2 bytes Double word 4 bytes Quad word 8 bytes Paragraph 16 bytes
22 Reserving Memory Examples L1 db 0 ;byte labeled L1 with initial value 0 L2 dw 1000 ;word labeled L2 with initial value 1000 L3 db b ;byte initialized to binary L4 db 12h ;byte initialized to hex 12 L5 db 17o ;byte initialized to octal 17 L6 dd 1A92h ;double word initialized to hex 1A92 L7 resb 1 ;1 un-initialized byte L8 db A ;byte initialized to ASCII for A (65) L9 db 0, 1, 2, 3 ;defines 4 bytes L10 db w, o, r, d, 0 ;defines a C string = word L11 db word, 0 ;same as L10 L12 times 100 db 0 ;equivalent to 100 (db 0) s L13 resw 100 ;reserve room for 100 words
23 Registers CPU data manipulation EAX, EBX, ECX, EDX general purpose Pointer Registers ESI, EDI and general purpose Stack EBP and ESP Segment Registers CS (code), DS (data), SS (stack), ES (extra) temporary register
24 Registers Program instruction pointer EIP Flag status EFLAGS Floating point manipulation floating point registers (see page 7 Carter)
25 Registers ESP Stack pointer EBP base pointer ESI index/general purpose EDI index/general purpose EAX counter for procedures (success =1, fail = 0)
26 Stack Frame Stack Frame contains the parameters, return information and local variables The area in stack memory between the base pointer EBP and the stack pointer ESP Created by pushing EBP to the stack and moving ESP into EBP. EBP is considered the anchor of the new stack frame. ESP is now free to move up and down the stack as needed
27 Back to BoilerPlate [main: push ebp mov ebp, esp push ebx push esi push edi ;push value in base pointer to stack ;move the stack pointer value into the base pointer register ;push the value in the base address reg to the stack ;push the value in the source index reg to the stack ;push the value in the destination index reg to the stack ;;; Your code here ] pop edi pop esi pop ebx mov esp, ebp pop ebp ret ;Now we restore the original values
28 MOV Reminder: Format: mov destination, source Data is source is copied to destination Examples: mov eax, 80 mov eax, ebx mov eax, [input]
29 JMP Reminder: Format: jmp label Begins execution at the next instruction after the label in the code Examples: label: ;some code jmp label ;the execution returns to the ;instruction immediately after ;the initial label
30 ADD Reminder: Format: add destination, source1, source2 Adds two values and places them in a destination Different format when implementing: add source1, source2 Same as source1 = source1 + source2 Examples: add ebx, ecx add ebx, 12
31 I/O in Linux Assembly List external files to be used Save the current registers by pushing onto the stack Use the stack for parameter passing to printf(), scanf(), and fgets() Adjust stack pointer ESP as necessary On completion of program restore registers by popping from the stack
32 External Files stdin standard file variable for input char *fgets(char *s, int size, FILE *stream); int printf(const char *format, ); int scanf(const char *format, );
33 int printf(const char *format, ) Write out the printf call in C style first Declare as extern printf Declare and initialize the base string: sshow db The string you entered was %s, 10, 0 Push the parameters in reverse order. Call printf Clean up the stack (add 4 for each push to ESP)
34 Formatting in printf() Format %c %d %e %f %g %i %o %s %u %x %% Description
35 Formatting in printf() Format Description %c Print a single character %d Print a decimal (base 10) number %e Print an exponential floating-point number %f Print a floating-point number %g Print a general-format floating-point number %i Print an integer in base 10 %o Print a number in octal (base 8) %s Print a string of characters %u Print an unsigned decimal (base 10) number %x Print a number in hexadecimal (base 16) %% Print a percent sign (\% also works)
36 Printing in Assembly: Example code: [section.text] extern printf global main main: ;boilerplate push dword phrase call printf add esp, 4 ;boilerplate [section.data] pharse db Space, the final frontier, 10, 0
37 int scanf(const char *format, ) Declare as extern scanf Declare a string that contains the format of the value to be input: iformat db %d, 10, 0 Reserve memory for each input to be used: intval resd 1 ;reserves 1 un-initialized double word Push the address of the reserved memory for the input on the stack Push the address of the address string on the stack. Call scanf Clean up the stack (add 4 for each push to ESP)
38 Scanning in Assembly: Example code: [section.text] extern scanf global main main: ;boilerplate push dword input push dword format call scanf add esp, 8 ;boilerplate [section.data] format db %d, 10, 0 [section.bss] input resd 1 ;push the address of the integer ;buffer ;push the address of the input ;format string ;clean up the stack
39 To compile: nasm f elf filename.asm gcc filename.o o exe_name./exe_name
40 Compiling: nasm f elf filename.asm Assembles filename.asm into an ELF object file named filename.o gcc filename.o o exe_name Links the object file to libraries that are needed (like C libraries)./exe_name This will execute your code
41 File Layout 4SXMo
42 Memory Layout (again)
43 Basic Instructions MOV ADD mov eax, 3 mov bx, ax ;store 3 into EAX register ;(3 is immediate operand) ;store the value of AX into ;the BX register add eax, 4 ;eax = eax + 4 add al, ah ;al = al + ah
44 Basic Instructions SUB sub bx, 10 ;bx = bx 10 sub ebx, edi ;ebx = ebx edi INC and DEC inc ecx dec dl ;ecx++ ;dl--
45 Bit of Practice Statements: 1. a = 2 + 3; 2. b = 7 + 8; 3. c = a + b; What assembly code produces this? What is the state of the registers through execution?
46 IMUL Instruction In unsigned integers, 255 = FF = * 255 = In signed integers, -1-1 is in two s complement -1 * -1 = 1 We need IMUL for signed integers.
47 IMUL IMUL source IMUL source, immed IMUL destination, source, immed IMUL destination, source
48 IMUL Example mov eax, [input] imul eax mov eax, 10 mov edx, 15 imul eax, edx imul ecx, eax, edx ;eax = user input ;eax = eax * eax ;eax = eax * edx ;ecx = eax * edx
49 Bit Significance Numerical concept of a significant digit is useful when singling out individual bits of a specific storage location. Ex. The binary number MSB = 1 and LSB = 0 The three MSBs is 110
50 Negative Number Representation As humans, the customary way to represent a negative number is to append a minus sign Put a in front of 3 makes a -3 This is called signed magnitude representation. How can we implement this on a computer? Implemented on the computer by allocating one bit of storage for a sign bit.
51 Negative Number Representation To change the sign, simply flip the bit that is representing the sign. This representation is not generally used for integers on the computer. Why? The reason is that in order to do addition with signed magnitude representation, we need to do it the way it is taught to kids:
52 Signed Magnitude Addition In order to add x and y, we have to consider cases: Case 1: If x and y are positive, Just add and make your answer positive. Case 2: If x and y are both negative, Remove the negative signs, add the numbers, and tack a minus sign onto the result. Case 3: If x and y have different signs, Subtract the smaller magnitude from the larger one, then tack the sign from the larger one to the result
53 Signed Magnitude Addition vs Easy Addition Implementing this method of addition with logic circuits on a chip would be a terrible waste of transistors! So how would we want to implement this? Implement with logic circuits to add x and y such that the sign doesn t matter! Just add them
54 Signed vs. Unsigned Numbers Sometimes arithmetic is done and it assumes that numbers are represented using the plain binary system; other times the system may assume two s complement Both systems are used It is up to the software to make the distinction This is why you see things like signed vs unsigned integers in programming langs.
55 Main Methods There are three main methods that computers represent signed numbers: 1. Signed Magnitude 2. One s Complement 3. Two s Complement All use the MSB to show if number is negative (1) or not (0) Two s Complement is used in most modern day computers
56 Signed Magnitude (Revisited) A technique to represent negative numbers in binary: 1. Take the binary representation of the number, 2. Flip the MSB, MSB = 1 represents a negative number. *Note: Very important to make sure to have enough digits to represent the negative-ness of the number. Ex: Using Signed Magnitude, represent the signed integer -19 How about the signed integer 5??
57 One s complement A technique to represent negative numbers in binary: 1. Take the binary representation of the number, 2. Flip all of the bits MSB = 1 represents a negative number. *Note: Very important to make sure to have enough digits to represent the negative-ness of the number. Ex: Using One s complement, represent the signed integer -19 How about the signed integer 5??
58 Two s complement A technique to represent negative numbers in binary: 1. Take the binary representation of the number, 2. Flip all of the bits, 3. Add 1 MSB = 1 represents a negative number. *Note: Very important to make sure to have enough digits to represent the negative-ness of the number. Ex: Using Two s complement, represent the signed integer -19 How about the signed integer 5??
59 Two s complement Arithmetic Class Demo! Double Negate? Check! Arithmetic? Check!
60 Sign Extension In assembly, all data has a specified size. Sometimes we may need to change the size to use it with other data Decreasing is the easiest! Simply remove the more significant bits of the data Does not work if number cannot be represented correctly in remaining bits All bits being removed must be 0 (for unsigned numbers) to be correct (For signed) all 1s or all 0s and a keep signed bit
61 Sign Extension Increasing the size of data is more complicated: In general, make new bits of expanded number 0 for an unsigned number For signed, we must expand the signed bit Remember this is important because it is up to the programmer to use the appropriate instructions. The computer is unaware if a number is signed or unsigned.
62 Directives Directives are Assembler specific NASM may be different from MASM Instruct or inform the Assembler to do something Not translated into machine code Common uses: Define constants Define memory to store data into Group memory into segments Conditionally include source code Include other files
63 Directives The equ directive Can be used to define a symbol. Symbols are named constants and their values can not be redefined later Syntax: symbol equ value The %define directive Similar to C s #define directive Commonly used to define constant macros Example: %define SIZE 100 mov eax, SIZE
64 Directives Data directives Used in data segments to define room for memory We see these in our.data and.bss sections: Two ways memory can be reserved: 1. Using RESX directives, where X is replaced with a letter that determines the size 2. DX, where again X is replaced with a letter that determines the size.
65 Labels Labels are used to refer to data in code. Two ways that a label can be used: If plain label is used, it is interpreted as the address (or offset) of the data. If the [label] is used, it is interpreted as the data at that address. (For my C-like language folks): You can think of a label as a pointer to the data and the [ ] dereferences the pointer just as the asterisk does in C
66 IDIV Instruction No special idiv instructions like there are with imul. General format: idiv source What do we get from a division operation? Quotient and remainder so where are these stored? In special registers!
67 IDIV Instruction EDX and EAX are the special registers used to store this information. Quotient is stored in EAX Remainder is stored in EDX Common mistake is to forget to initialize EDX before division. Also, cannot divide by immediate value! Call CDQ command prior to IDIV, this initializes EDX by sign extension cdq idiv ecx ;initalize edx by sign extend ;edx:eax / ecx
68 Now let s take a minute Scanf Printf getchar Let s look at examples of using all of these <Another In-Class Demo Here>!
69 Control Structures High level languages provide high level control structures that control the sequence of execution. Examples if, if-else, while, do while, for, etc Assemble does not provide such complex structures. Uses the infamous goto method instead
70 Control Structures So is it still possible to implement these control structure in assembly? Yes! Basic Procedure: Design the program logic using high-level control structures Translate the design into the appropriate assembly language (Just like a compiler would do!!)
71 Comparisons Comparing data drives effects that control structures have on instruction execution In assembly, results from a comparison is stored in a special register FLAGS The CMP instruction performs comparisons.
72 CMP instruction FLAGS is set based on the difference of the two operands of the CMP instruction The operands are subtracted and the FLAGS register is set based on the result. Note:: the result is NOT stored anywhere If needed, you will have to use the sub instruction yourself.
73 CMP and FLAGS For unsigned ints, there are two flags that are important: the zero (ZF) and the carry (CF) flags. These are merely bits in the FLAGS register The zero flag is set (1) if the resulting difference would be zero. The carry flag is used as a borrow flag for subtraction.
74 CMP Example Consider the following instruction: cmp vleft, vright What are the values of ZF and CF?
75 CMP and FLAGS For signed ints, there are three flags that are important: the zero (ZF), the overflow (OF) and the sign (SF) flags. These are merely bits in the FLAGS register The zero flag works the same as before. The overflow flag is set if result from an op overflows. The sign flag is set if the result of an op is neg.
76 CMP Example (Again) Consider the following instruction using signed ints: cmp vleft, vright What are the values of ZF, OF, and SF?
77 Be careful!! ***Do Not Forget: CMP is not the only instruction that can change the FLAGS register
78 Branch Instructions These can transfer execution to arbitrary points of a program. They essentially act like a goto Two types of branches: Unconditional Conditional
79 Branch Instructions Unconditional branches act just like gotos Branch is always taken Conditional branches not like goto May or may not take the branch Depends on the state of the FLAGS register On branch not taken, control passes to next instruction
80 JMP instruction Short for jump Makes unconditional branches Takes a single argument, the code label, to the instruction you wish to branch to So what about the conditional jumps? This instruction comes with several variations
81 Variations of JMP There are several variations of the jump instruction: SHORT NEAR FAR
82 Variations of JMP SHORT Limited range Can only move up or down 128 bytes in memory Advantage: uses less memory than the others Uses a single byte to store the displacement, number of bytes to move ahead or behind in memory, of the jump Syntax: short jmp label
83 Variations of JMP NEAR Default type for both branch types Used to jump to any location in a code segment FAR Allows control to move to another code segment Very rare variation of jump to use
84 JMP labels Valid labels follow same rules as data labels Labels are defined by placing them in the code segment in front of the statement they label. A colon is placed as the end as its point of definition Note: the colon is not part of the label name
85 Conditional Branch Instructions Instruction JZ JNZ JO JNO JS JNS JC JNC JP JNP Branch Condition
86 Conditional Branch Instructions Instruction JZ JNZ JO JNO JS JNS JC JNC JP JNP Branch Condition Branches only if ZF is set Branches only if ZF is unset Branches only if OF is set Branches only if OF is unset Branches only if SF is set Branches only if SF is unset Branches only if CF is set Branches only if CF is unset Branches only if PF is set Branches only if PF is unset
87 Comparison Example 1 The following pseudocode: Could be written in assembly as: if(eax == 0) EBX = 1; else EBX = 2; cmp eax, 0 jz thenblock mov ebx, 2 jmp next thenblock: mov ebx, 1 next:
88 A Closer look cmp eax, 0 jz thenblock mov ebx, 2 jmp next thenblock: next: mov ebx, 1 ; set flags ;(ZF set if eax 0 = 0) ;if ZF is set branch to thenblock ;ELSE part of IF ;jump over THEN part of IF ;THEN part of IF
89 Comparison Example 2 The following pseudocode: if(eax >= 5) EBX = 1; else EBX = 2; Could be written in assembly as: cmp eax, 5 js signon jo elseblock jmp thenblock signon: jo thenblock elseblock: mov ebx, 2 jmp next thenblock: mov ebx, 1 next:
90 A Closer look cmp eax, 5 js signon jo elseblock jmp thenblock signon: jo thenblock elseblock: mov ebx, 2 jmp next thenblock: mov ebx, 1 next: ;goto signon if SF=1 ;goto elseblock if OF=1 & SF=0 ;goto thenblock if SF=0 & OF=0 ;goto thenblock if SF=1 & OF=1
91 Other Branch Instructions The above instructions can make code a bit awkward, even for assembly Fortunately, there are additional branch instructions that make testing things easier!! --YAY! These instructions come in both signed and unsigned forms
92 Signed and Unsigned Comparison Instructions Signed Unsigned JE Branches if vleft = vright JE Branches if vleft = vright JNE Branches if vleft vright JNE Branches if vleft vright JL, JNGE Branches if vleft < vright JB, JNAE Branches if vleft < vright JLE, JNG Branches if vleft vright JBE, JNA Branches if vleft vright JG, JNLE Branches if vleft > vright JA, JNBE Branches if vleft > vright JGE, JNL Branches if vleft vright JAE, JNA Branches if vleft vright Note: unsigned branches use A for above and B for below instead of L and G
93 Comparison Example 2 (revisited) The following pseudocode: if(eax >= 5) EBX = 1; else EBX = 2; Could be written in assembly as: cmp eax, 5 jge thenblock mov ebx, 2 jmp next thenblock: next: mov ebx, 1
94 Comparison Example 2 (revisited) Version1: cmp eax, 5 js signon jo elseblock jmp thenblock signon: jo thenblock elseblock: mov ebx, 2 jmp next thenblock: mov ebx, 1 next: Version2: cmp eax, 5 jge thenblock mov ebx, 2 jmp next thenblock: mov ebx, 1 next:
95 The loop instructions There are several instructions to implement for-like loops: LOOP LOOPE, LOOPZ LOOPNE, LOOPNZ Each takes a code label as its single operand The last two are useful for sequential search loops
96 The loop instructions LOOP Decrements ECX If ECX 0, it branches to label LOOPE, LOOPZ Decrements ECX (FLAGS register is not modified) If ECX 0 and ZF = 1, then branches LOOPNE, LOOPNZ Decrements ECX (FLAGS unchanged) If ECX 0 and ZF = 0, then branches
97 Loop Example 1 The following pseudocode: Could be written in assembly as: sum = 0; for(i=10; i>0; i--) sum += i; mov eax, 0 mov ecx, 10 loop_start: add eax, ecx loop loop_start
98 Translating Standard Control Structures (IF-ELSE statements) The following pseudo-code: if( condition ) then_block; else else_block Could be implemented as: ;code to set FLAGS jxx else_block ;code for then block jmp endif else_block: ;code for else block endif: Select xx in jxx so that the branch is taken if condition is false
99 Translating Standard Control Structures (IF statements) The following pseudo-code: Could be implemented as: if( condition ) then_block; ;code to set FLAGS jxx endif ;code for then block endif: Select xx in jxx so that the branch is taken if condition is false
100 Translating Standard Control Structures (While-loop statements) The following pseudo-code: while(condition) { } body of loop; Could be implemented as: while: ;code to set FLAGS ;based on condition jxx endwhile ;body of loop jmp while endwhile: Select xx in jxx so that the branch is taken if condition is false
101 Translating Standard Control Structures (do while-loop statements) The following pseudo-code: Could be implemented as: do { body of loop; }while(condition); do: ;body of loop ;code to set FLAGS ;based on condition jxx do Select xx in jxx so that the branch is taken if condition is false
102 Problem to think about: Exercise: Write the assembly code needed to find the prime numbers up to a given number.
Complete 8086 instruction set
Page 1 of 53 Complete 8086 instruction set Quick reference: AAA AAD AAM AAS ADC ADD AND CALL CBW CLC CLD CLI CMC CMP CMPSB CMPSW CWD DAA DAS DEC DIV HLT IDIV IMUL IN INC INT INTO I JA JAE JB JBE JC JCXZ
More informationx64 Cheat Sheet Fall 2015
CS 33 Intro Computer Systems Doeppner x64 Cheat Sheet Fall 2015 1 x64 Registers x64 assembly code uses sixteen 64-bit registers. Additionally, the lower bytes of some of these registers may be accessed
More informationCS61: Systems Programing and Machine Organization
CS61: Systems Programing and Machine Organization Fall 2009 Section Notes for Week 2 (September 14 th - 18 th ) Topics to be covered: I. Binary Basics II. Signed Numbers III. Architecture Overview IV.
More informationInstruction Set Architecture
CS:APP Chapter 4 Computer Architecture Instruction Set Architecture Randal E. Bryant adapted by Jason Fritts http://csapp.cs.cmu.edu CS:APP2e Hardware Architecture - using Y86 ISA For learning aspects
More informationFaculty of Engineering Student Number:
Philadelphia University Student Name: Faculty of Engineering Student Number: Dept. of Computer Engineering Final Exam, First Semester: 2012/2013 Course Title: Microprocessors Date: 17/01//2013 Course No:
More informationCS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08
CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 20: Stack Frames 7 March 08 CS 412/413 Spring 2008 Introduction to Compilers 1 Where We Are Source code if (b == 0) a = b; Low-level IR code
More informationA Tiny Guide to Programming in 32-bit x86 Assembly Language
CS308, Spring 1999 A Tiny Guide to Programming in 32-bit x86 Assembly Language by Adam Ferrari, ferrari@virginia.edu (with changes by Alan Batson, batson@virginia.edu and Mike Lack, mnl3j@virginia.edu)
More informationX86-64 Architecture Guide
X86-64 Architecture Guide For the code-generation project, we shall expose you to a simplified version of the x86-64 platform. Example Consider the following Decaf program: class Program { int foo(int
More informationUnpacked BCD Arithmetic. BCD (ASCII) Arithmetic. Where and Why is BCD used? From the SQL Server Manual. Packed BCD, ASCII, Unpacked BCD
BCD (ASCII) Arithmetic The Intel Instruction set can handle both packed (two digits per byte) and unpacked BCD (one decimal digit per byte) We will first look at unpacked BCD Unpacked BCD can be either
More information64-Bit NASM Notes. Invoking 64-Bit NASM
64-Bit NASM Notes The transition from 32- to 64-bit architectures is no joke, as anyone who has wrestled with 32/64 bit incompatibilities will attest We note here some key differences between 32- and 64-bit
More informationAssembly Language Tutorial
Assembly Language Tutorial ASSEMBLY LANGUAGE TUTORIAL by tutorialspoint.com tutorialspoint.com i ABOUT THE TUTORIAL Assembly Programming Tutorial Assembly language is a low-level programming language for
More informationLecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com
CSCI-UA.0201-003 Computer Systems Organization Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Some slides adapted (and slightly modified)
More informationOct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8
ECE Department Summer LECTURE #5: Number Systems EEL : Digital Logic and Computer Systems Based on lecture notes by Dr. Eric M. Schwartz Decimal Number System: -Our standard number system is base, also
More informationPC Assembly Language. Paul A. Carter
PC Assembly Language Paul A. Carter November 20, 2001 Copyright c 2001 by Paul Carter This may be reproduced and distributed in its entirety (including this authorship, copyright and permission notice),
More informationCS:APP Chapter 4 Computer Architecture Instruction Set Architecture. CS:APP2e
CS:APP Chapter 4 Computer Architecture Instruction Set Architecture CS:APP2e Instruction Set Architecture Assembly Language View Processor state Registers, memory, Instructions addl, pushl, ret, How instructions
More informationAbout the Tutorial. Audience. Prerequisites. Copyright & Disclaimer
About the Tutorial Assembly language is a low-level programming language for a computer or other programmable device specific to a particular computer architecture in contrast to most high-level programming
More informationIntel 8086 architecture
Intel 8086 architecture Today we ll take a look at Intel s 8086, which is one of the oldest and yet most prevalent processor architectures around. We ll make many comparisons between the MIPS and 8086
More informationMachine-Level Programming II: Arithmetic & Control
Mellon Machine-Level Programming II: Arithmetic & Control 15-213 / 18-213: Introduction to Computer Systems 6 th Lecture, Jan 29, 2015 Instructors: Seth Copen Goldstein, Franz Franchetti, Greg Kesden 1
More informationComputer Science 281 Binary and Hexadecimal Review
Computer Science 281 Binary and Hexadecimal Review 1 The Binary Number System Computers store everything, both instructions and data, by using many, many transistors, each of which can be in one of two
More informationEmbedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C
Embedded Systems A Review of ANSI C and Considerations for Embedded C Programming Dr. Jeff Jackson Lecture 2-1 Review of ANSI C Topics Basic features of C C fundamentals Basic data types Expressions Selection
More informationInstruction Set Architecture (ISA)
Instruction Set Architecture (ISA) * Instruction set architecture of a machine fills the semantic gap between the user and the machine. * ISA serves as the starting point for the design of a new machine
More informationBCD (ASCII) Arithmetic. Where and Why is BCD used? Packed BCD, ASCII, Unpacked BCD. BCD Adjustment Instructions AAA. Example
BCD (ASCII) Arithmetic We will first look at unpacked BCD which means strings that look like '4567'. Bytes then look like 34h 35h 36h 37h OR: 04h 05h 06h 07h x86 processors also have instructions for packed
More informationChapter 7D The Java Virtual Machine
This sub chapter discusses another architecture, that of the JVM (Java Virtual Machine). In general, a VM (Virtual Machine) is a hypothetical machine (implemented in either hardware or software) that directly
More informationMACHINE ARCHITECTURE & LANGUAGE
in the name of God the compassionate, the merciful notes on MACHINE ARCHITECTURE & LANGUAGE compiled by Jumong Chap. 9 Microprocessor Fundamentals A system designer should consider a microprocessor-based
More informationHacking Techniques & Intrusion Detection. Ali Al-Shemery arabnix [at] gmail
Hacking Techniques & Intrusion Detection Ali Al-Shemery arabnix [at] gmail All materials is licensed under a Creative Commons Share Alike license http://creativecommonsorg/licenses/by-sa/30/ # whoami Ali
More informationOverview of IA-32 assembly programming. Lars Ailo Bongo University of Tromsø
Overview of IA-32 assembly programming Lars Ailo Bongo University of Tromsø Contents 1 Introduction... 2 2 IA-32 assembly programming... 3 2.1 Assembly Language Statements... 3 2.1 Modes...4 2.2 Registers...4
More informationAssembly Language: Function Calls" Jennifer Rexford!
Assembly Language: Function Calls" Jennifer Rexford! 1 Goals of this Lecture" Function call problems:! Calling and returning! Passing parameters! Storing local variables! Handling registers without interference!
More informationComputer Organization and Architecture
Computer Organization and Architecture Chapter 11 Instruction Sets: Addressing Modes and Formats Instruction Set Design One goal of instruction set design is to minimize instruction length Another goal
More informationThe string of digits 101101 in the binary number system represents the quantity
Data Representation Section 3.1 Data Types Registers contain either data or control information Control information is a bit or group of bits used to specify the sequence of command signals needed for
More informationPROBLEMS (Cap. 4 - Istruzioni macchina)
98 CHAPTER 2 MACHINE INSTRUCTIONS AND PROGRAMS PROBLEMS (Cap. 4 - Istruzioni macchina) 2.1 Represent the decimal values 5, 2, 14, 10, 26, 19, 51, and 43, as signed, 7-bit numbers in the following binary
More informationThe programming language C. sws1 1
The programming language C sws1 1 The programming language C invented by Dennis Ritchie in early 1970s who used it to write the first Hello World program C was used to write UNIX Standardised as K&C (Kernighan
More informationTest Driven Development in Assembler a little story about growing software from nothing
Test Driven Development in Assembler a little story about growing software from nothing Olve Maudal During the last decade Test-Driven Development has become an established practice for developing software
More informationAdvanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2
Lecture Handout Computer Architecture Lecture No. 2 Reading Material Vincent P. Heuring&Harry F. Jordan Chapter 2,Chapter3 Computer Systems Design and Architecture 2.1, 2.2, 3.2 Summary 1) A taxonomy of
More informationIntroduction. Application Security. Reasons For Reverse Engineering. This lecture. Java Byte Code
Introduction Application Security Tom Chothia Computer Security, Lecture 16 Compiled code is really just data which can be edit and inspected. By examining low level code protections can be removed and
More informationInformatica e Sistemi in Tempo Reale
Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 25, 2010 G. Lipari (Scuola Superiore Sant Anna)
More informationName: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.
Name: Class: Date: Exam #1 - Prep True/False Indicate whether the statement is true or false. 1. Programming is the process of writing a computer program in a language that the computer can respond to
More informationSources: On the Web: Slides will be available on:
C programming Introduction The basics of algorithms Structure of a C code, compilation step Constant, variable type, variable scope Expression and operators: assignment, arithmetic operators, comparison,
More informationMACHINE INSTRUCTIONS AND PROGRAMS
CHAPTER 2 MACHINE INSTRUCTIONS AND PROGRAMS CHAPTER OBJECTIVES In this chapter you will learn about: Machine instructions and program execution, including branching and subroutine call and return operations
More informationNumber Representation
Number Representation CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Topics to be Discussed How are numeric data
More informationReturn-oriented programming without returns
Faculty of Computer Science Institute for System Architecture, Operating Systems Group Return-oriented programming without urns S. Checkoway, L. Davi, A. Dmitrienko, A. Sadeghi, H. Shacham, M. Winandy
More informationBuffer Overflows. Security 2011
Buffer Overflows Security 2011 Memory Organiza;on Topics Kernel organizes memory in pages Typically 4k bytes Processes operate in a Virtual Memory Space Mapped to real 4k pages Could live in RAM or be
More informationSystems Design & Programming Data Movement Instructions. Intel Assembly
Intel Assembly Data Movement Instruction: mov (covered already) push, pop lea (mov and offset) lds, les, lfs, lgs, lss movs, lods, stos ins, outs xchg, xlat lahf, sahf (not covered) in, out movsx, movzx
More informationHigh-speed image processing algorithms using MMX hardware
High-speed image processing algorithms using MMX hardware J. W. V. Miller and J. Wood The University of Michigan-Dearborn ABSTRACT Low-cost PC-based machine vision systems have become more common due to
More informationCS201: Architecture and Assembly Language
CS201: Architecture and Assembly Language Lecture Three Brendan Burns CS201: Lecture Three p.1/27 Arithmetic for computers Previously we saw how we could represent unsigned numbers in binary and how binary
More information1 The Java Virtual Machine
1 The Java Virtual Machine About the Spec Format This document describes the Java virtual machine and the instruction set. In this introduction, each component of the machine is briefly described. This
More informationComputer Organization and Assembly Language
Computer Organization and Assembly Language Lecture 8 - Strings and Arrays Introduction We already know that assembly code will execute significantly faster than code written in a higher-level language
More information8. MACROS, Modules, and Mouse
8. MACROS, Modules, and Mouse Background Macros, Modules and the Mouse is a combination of concepts that will introduce you to modular programming while learning how to interface with the mouse. Macros
More informationEE 261 Introduction to Logic Circuits. Module #2 Number Systems
EE 261 Introduction to Logic Circuits Module #2 Number Systems Topics A. Number System Formation B. Base Conversions C. Binary Arithmetic D. Signed Numbers E. Signed Arithmetic F. Binary Codes Textbook
More informationASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)
ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6) 1 COMPUTER LANGUAGES In order for a computer to be able to execute a program, the program must first be present
More informationAn Introduction to Assembly Programming with the ARM 32-bit Processor Family
An Introduction to Assembly Programming with the ARM 32-bit Processor Family G. Agosta Politecnico di Milano December 3, 2011 Contents 1 Introduction 1 1.1 Prerequisites............................. 2
More information2) Write in detail the issues in the design of code generator.
COMPUTER SCIENCE AND ENGINEERING VI SEM CSE Principles of Compiler Design Unit-IV Question and answers UNIT IV CODE GENERATION 9 Issues in the design of code generator The target machine Runtime Storage
More informationSoftware Vulnerabilities
Software Vulnerabilities -- stack overflow Code based security Code based security discusses typical vulnerabilities made by programmers that can be exploited by miscreants Implementing safe software in
More informationNumbering Systems. InThisAppendix...
G InThisAppendix... Introduction Binary Numbering System Hexadecimal Numbering System Octal Numbering System Binary Coded Decimal (BCD) Numbering System Real (Floating Point) Numbering System BCD/Binary/Decimal/Hex/Octal
More informationStack Overflows. Mitchell Adair
Stack Overflows Mitchell Adair Outline Why? What? There once was a VM Virtual Memory Registers Stack stack1, stack2, stack3 Resources Why? Real problem Real money Real recognition Still prevalent Very
More informationTo convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:
Binary Numbers In computer science we deal almost exclusively with binary numbers. it will be very helpful to memorize some binary constants and their decimal and English equivalents. By English equivalents
More informationCDA 3200 Digital Systems. Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012
CDA 3200 Digital Systems Instructor: Dr. Janusz Zalewski Developed by: Dr. Dahai Guo Spring 2012 Outline Data Representation Binary Codes Why 6-3-1-1 and Excess-3? Data Representation (1/2) Each numbering
More informationAbysssec Research. 1) Advisory information. 2) Vulnerable version
Abysssec Research 1) Advisory information Title Version Discovery Vendor Impact Contact Twitter CVE : Apple QuickTime FlashPix NumberOfTiles Remote Code Execution Vulnerability : QuickTime player 7.6.5
More informationMICROPROCESSOR AND MICROCOMPUTER BASICS
Introduction MICROPROCESSOR AND MICROCOMPUTER BASICS At present there are many types and sizes of computers available. These computers are designed and constructed based on digital and Integrated Circuit
More information1 Classical Universal Computer 3
Chapter 6: Machine Language and Assembler Christian Jacob 1 Classical Universal Computer 3 1.1 Von Neumann Architecture 3 1.2 CPU and RAM 5 1.3 Arithmetic Logical Unit (ALU) 6 1.4 Arithmetic Logical Unit
More informationOff-by-One exploitation tutorial
Off-by-One exploitation tutorial By Saif El-Sherei www.elsherei.com Introduction: I decided to get a bit more into Linux exploitation, so I thought it would be nice if I document this as a good friend
More informationZ80 Instruction Set. Z80 Assembly Language
75 Z80 Assembly Language The assembly language allows the user to write a program without concern for memory addresses or machine instruction formats. It uses symbolic addresses to identify memory locations
More informationLecture 27 C and Assembly
Ananda Gunawardena Lecture 27 C and Assembly This is a quick introduction to working with x86 assembly. Some of the instructions and register names must be check for latest commands and register names.
More informationInstruction Set Architecture
Instruction Set Architecture Consider x := y+z. (x, y, z are memory variables) 1-address instructions 2-address instructions LOAD y (r :=y) ADD y,z (y := y+z) ADD z (r:=r+z) MOVE x,y (x := y) STORE x (x:=r)
More informationThe 80x86 Instruction Set
Thi d t t d ith F M k 4 0 2 The 80x86 Instruction Set Chapter Six Until now, there has been little discussion of the instructions available on the 80x86 microprocessor. This chapter rectifies this situation.
More informationNumeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:
Numeral Systems Which number is larger? 25 8 We need to distinguish between numbers and the symbols that represent them, called numerals. The number 25 is larger than 8, but the numeral 8 above is larger
More information2011, The McGraw-Hill Companies, Inc. Chapter 3
Chapter 3 3.1 Decimal System The radix or base of a number system determines the total number of different symbols or digits used by that system. The decimal system has a base of 10 with the digits 0 through
More informationBinary Representation. Number Systems. Base 10, Base 2, Base 16. Positional Notation. Conversion of Any Base to Decimal.
Binary Representation The basis of all digital data is binary representation. Binary - means two 1, 0 True, False Hot, Cold On, Off We must be able to handle more than just values for real world problems
More informationNotes on Assembly Language
Notes on Assembly Language Brief introduction to assembly programming The main components of a computer that take part in the execution of a program written in assembly code are the following: A set of
More informationA single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc
Other architectures Example. Accumulator-based machines A single register, called the accumulator, stores the operand before the operation, and stores the result after the operation. Load x # into acc
More informationB.Sc.(Computer Science) and. B.Sc.(IT) Effective From July 2011
NEW Detailed Syllabus of B.Sc.(Computer Science) and B.Sc.(IT) Effective From July 2011 SEMESTER SYSTEM Scheme & Syllabus for B.Sc. (CS) Pass and Hons. Course Effective from July 2011 and onwards CLASS
More informationBase Conversion written by Cathy Saxton
Base Conversion written by Cathy Saxton 1. Base 10 In base 10, the digits, from right to left, specify the 1 s, 10 s, 100 s, 1000 s, etc. These are powers of 10 (10 x ): 10 0 = 1, 10 1 = 10, 10 2 = 100,
More informationBachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)
Unit- I Introduction to c Language: C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating
More informationCPEN 214 - Digital Logic Design Binary Systems
CPEN 4 - Digital Logic Design Binary Systems C. Gerousis Digital Design 3 rd Ed., Mano Prentice Hall Digital vs. Analog An analog system has continuous range of values A mercury thermometer Vinyl records
More informationLecture 2. Binary and Hexadecimal Numbers
Lecture 2 Binary and Hexadecimal Numbers Purpose: Review binary and hexadecimal number representations Convert directly from one base to another base Review addition and subtraction in binary representations
More informationThe C Programming Language course syllabus associate level
TECHNOLOGIES The C Programming Language course syllabus associate level Course description The course fully covers the basics of programming in the C programming language and demonstrates fundamental programming
More informationChapter 5 Instructor's Manual
The Essentials of Computer Organization and Architecture Linda Null and Julia Lobur Jones and Bartlett Publishers, 2003 Chapter 5 Instructor's Manual Chapter Objectives Chapter 5, A Closer Look at Instruction
More informationDivide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1
Divide: Paper & Pencil Computer Architecture ALU Design : Division and Floating Point 1001 Quotient Divisor 1000 1001010 Dividend 1000 10 101 1010 1000 10 (or Modulo result) See how big a number can be
More informationChapter 2. Binary Values and Number Systems
Chapter 2 Binary Values and Number Systems Numbers Natural numbers, a.k.a. positive integers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645, 32 Negative numbers A
More informationPART B QUESTIONS AND ANSWERS UNIT I
PART B QUESTIONS AND ANSWERS UNIT I 1. Explain the architecture of 8085 microprocessor? Logic pin out of 8085 microprocessor Address bus: unidirectional bus, used as high order bus Data bus: bi-directional
More information6 3 4 9 = 6 10 + 3 10 + 4 10 + 9 10
Lesson The Binary Number System. Why Binary? The number system that you are familiar with, that you use every day, is the decimal number system, also commonly referred to as the base- system. When you
More informationCharacter Translation Methods
Supplement to: Irvine, Kip R. Assembly Language for Intel-Based Computers, 4th Edition. This file may be duplicated or printed for classroom use, as long as the author name, book title, and copyright notice
More informationUsing the RDTSC Instruction for Performance Monitoring
Using the Instruction for Performance Monitoring http://developer.intel.com/drg/pentiumii/appnotes/pm1.htm Using the Instruction for Performance Monitoring Information in this document is provided in connection
More informationAdministration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers
CS 4 Introduction to Compilers ndrew Myers Cornell University dministration Prelim tomorrow evening No class Wednesday P due in days Optional reading: Muchnick 7 Lecture : Instruction scheduling pr 0 Modern
More informationInstruction Set Design
Instruction Set Design Instruction Set Architecture: to what purpose? ISA provides the level of abstraction between the software and the hardware One of the most important abstraction in CS It s narrow,
More informationSystems I: Computer Organization and Architecture
Systems I: Computer Organization and Architecture Lecture 2: Number Systems and Arithmetic Number Systems - Base The number system that we use is base : 734 = + 7 + 3 + 4 = x + 7x + 3x + 4x = x 3 + 7x
More informationProgramming from the Ground Up. Jonathan Bartlett
Programming from the Ground Up Jonathan Bartlett Programming from the Ground Up by Jonathan Bartlett Copyright 2002 by Jonathan Bartlett Permission is granted to copy, distribute and/or modify this document
More informationBinary Representation
Binary Representation The basis of all digital data is binary representation. Binary - means two 1, 0 True, False Hot, Cold On, Off We must tbe able to handle more than just values for real world problems
More informationStack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings: 9.1-9.
Code Generation I Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings: 9.1-9.7 Stack Machines A simple evaluation model No variables
More informationChapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC
Chapter 2 Topics 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC See Appendix C for Assembly language information. 2.4
More informationInstruction Set Architecture. or How to talk to computers if you aren t in Star Trek
Instruction Set Architecture or How to talk to computers if you aren t in Star Trek The Instruction Set Architecture Application Compiler Instr. Set Proc. Operating System I/O system Instruction Set Architecture
More informationCompiler Construction
Compiler Construction Lecture 1 - An Overview 2003 Robert M. Siegfried All rights reserved A few basic definitions Translate - v, a.to turn into one s own language or another. b. to transform or turn from
More informationOutline. Lecture 3. Basics. Logical vs. physical memory. 8086 physical memory. x86 byte ordering
Outline Lecture 3 bout Memory ddressing memory Data types MOV instruction ddressing modes Instruction format Dr. Dimitrios S. Nikolopoulos SL/UIU Basics Logical vs. physical memory Memory in the x processors
More informationMotorola 8- and 16-bit Embedded Application Binary Interface (M8/16EABI)
Motorola 8- and 16-bit Embedded Application Binary Interface (M8/16EABI) SYSTEM V APPLICATION BINARY INTERFACE Motorola M68HC05, M68HC08, M68HC11, M68HC12, and M68HC16 Processors Supplement Version 2.0
More informationHC12 Assembly Language Programming
HC12 Assembly Language Programming Programming Model Addressing Modes Assembler Directives HC12 Instructions Flow Charts 1 Assembler Directives In order to write an assembly language program it is necessary
More informationCPU Organization and Assembly Language
COS 140 Foundations of Computer Science School of Computing and Information Science University of Maine October 2, 2015 Outline 1 2 3 4 5 6 7 8 Homework and announcements Reading: Chapter 12 Homework:
More informationComputer Architectures
Computer Architectures 2. Instruction Set Architectures 2015. február 12. Budapest Gábor Horváth associate professor BUTE Dept. of Networked Systems and Services ghorvath@hit.bme.hu 2 Instruction set architectures
More informationSoftware Fingerprinting for Automated Malicious Code Analysis
Software Fingerprinting for Automated Malicious Code Analysis Philippe Charland Mission Critical Cyber Security Section October 25, 2012 Terms of Release: This document is approved for release to Defence
More informationCPU Organisation and Operation
CPU Organisation and Operation The Fetch-Execute Cycle The operation of the CPU 1 is usually described in terms of the Fetch-Execute cycle. 2 Fetch-Execute Cycle Fetch the Instruction Increment the Program
More information612 CHAPTER 11 PROCESSOR FAMILIES (Corrisponde al cap. 12 - Famiglie di processori) PROBLEMS
612 CHAPTER 11 PROCESSOR FAMILIES (Corrisponde al cap. 12 - Famiglie di processori) PROBLEMS 11.1 How is conditional execution of ARM instructions (see Part I of Chapter 3) related to predicated execution
More informationChapter 1: Digital Systems and Binary Numbers
Chapter 1: Digital Systems and Binary Numbers Digital age and information age Digital computers general purposes many scientific, industrial and commercial applications Digital systems telephone switching
More information