X86 assembly language and c fundamentals

807 266 0
X86 assembly language and c fundamentals

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Assembly language programming requires knowledge of number representations, as well as the architecture of the computer on which the language is being used After covering the binary, octal, decimal, and hexadecimal number systems, the book presents the general architecture of the X86 microprocessor, individual addressing modes, stack operations, procedures, arrays, macros, and input/output operations It highlights the most commonly used X86 assembly language instructions, including data transfer, branching and looping, logic, shift and rotate, and string instructions, as well as fixed-point, binary-coded decimal (BCD), and floating-point arithmetic instructions Written for students in computer science and electrical, computer, and software engineering, the book assumes a basic background in C programming, digital logic design, and computer architecture Designed as a tutorial, this comprehensive and self-contained text offers a solid foundation in assembly language for anyone working with the design of digital hardware an informa business www.taylorandfrancisgroup.com 6000 Broken Sound Parkway, NW Suite 300, Boca Raton, FL 33487 711 Third Avenue New York, NY 10017 Park Square, Milton Park Abingdon, Oxon OX14 4RN, UK K16377 X86 Assembly Language and C Fundamentals The predominant language used in embedded microprocessors, assembly language lets you write programs that are typically faster and more compact than programs written in a highlevel language and provide greater control over the program applications Focusing on the languages used in X86 microprocessors, X86 Assembly Language and C Fundamentals explains how to write programs in the X86 assembly language, the C programming language, and X86 assembly language modules embedded in a C program A wealth of program design examples, including the complete code and outputs, help you grasp the concepts more easily Where needed, the book also details the theory behind the design Cavanagh Computer Science & Engineering X86 Assembly C Language and Fundamentals Joseph Cavanagh ISBN: 978-1-4665-6824-2 90000 781466 568242 www.crcpress.com K16377 cvr mech.indd 11/15/12 10:15 AM X86 Assembly Language and C Fundamentals This page intentionally left blank X86 Assembly Language and C Fundamentals Joseph Cavanagh Santa Clara University, Santa Clara, California CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2013 by © 2013 by © 2013 by © 2013 by © 2013 by © 2013 by © 2013 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S Government works Version Date: 20130109 International Standard Book Number-13: 978-1-4665-6825-9 (eBook - PDF) This book contains information obtained from authentic and highly regarded sources Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint Except as permitted under U.S Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers For permission to photocopy or use material electronically from this work, please access www.copyright.com (http:// www.copyright.com/) or contact the Copyright Clearance Center, Inc (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400 CCC is a not-for-profit organization that provides licenses and registration for a variety of users For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com By the same author: COMPUTER ARITHMETIC and Verilog HDL Fundamentals DIGITAL DESIGN and Verilog HDL Fundamentals VERILOG HDL: Digital Design and Modeling SEQUENTIAL LOGIC: Analysis and Synthesis DIGITAL COMPUTER ARITHMETIC: Design and Implementation THE COMPUTER CONSPIRACY A novel This page intentionally left blank To Dr Daniel W Lewis Professor, Computer Engineering Department, Santa Clara University, for his many years of continued encouragement, support, and friendship This page intentionally left blank CONTENTS Preface xvii Chapter Number Systems and Number Representations Number Systems 1.1.1 Binary Number System 1.1.2 Octal Number System 1.1.3 Decimal Number System 1.1.4 Hexadecimal Number System 1.1.5 Arithmetic Operations 1.1.6 Conversion between Radices Number Representations 1.2.1 Sign Magnitude 1.2.2 Diminished-Radix Complement 1.2.3 Radix Complement 1.2.4 Arithmetic Operations Problems 12 20 26 26 28 31 32 47 1.1 1.2 1.3 Chapter X86 Processor Architecture 51 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 General Architecture Arithmetic and Logic Unit Control Unit Memory Unit 2.4.1 Main Memory 2.4.2 Hamming Code 2.4.3 Cache Memory Input/Output 2.5.1 Tape Drives 2.5.2 Disk Drives Register Set 2.6.1 General-Purpose Registers 2.6.2 Segment Registers 2.6.3 EFLAGS Register 2.6.4 Instruction Pointer 2.6.5 Floating-Point Registers Translation Lookaside Buffer The Assembler 2.8.1 The Assembly Process Problems 51 52 54 55 55 56 61 65 65 66 68 70 71 72 74 75 80 81 82 83 768 Appendix B Chapter 16 Operations 16.3 Answers to Select Problems Interrupts and Input/Output Explain why interrupt breakpoints occur only at the end of an instruction cycle If interrupt breakpoints occurred during an instruction cycle, for example, at the end of a CPU cycle, then control would be transferred to the ISR and the current instruction would not be completed Therefore, interrupt breakpoints occur only at the end of an instruction cycle, that is, when the current instruction has completed execution 16.7 Explain why DMA access to main memory has a higher priority than central processing units access to main memory Input/output devices equipped with DMA hardware have highest priority over central processing units when transferring data to or from main memory, because they are inherently slower and cannot have the data transfer stopped temporarily Thus, data would be lost Disk drives and tape drives are examples of I/O subsystems in this category Chapter 17 Additional Programming Examples Chapter 17 Examples 17.2 769 Additional Programming Write a program in assembly language — not embedded in a C program — that counts the number of times that a number occurs in an array of eight numbers The first number entered from the keyboard is the number that is being compared to the remaining eight numbers Display the result ;count_occurrence2.asm ;count the number of times that a number ;occurs in an array of eight numbers ;the first number entered from the keyboard ;is the number that is being compared to ;the remaining eight numbers ; .STACK ; .DATA PARLST LABEL BYTE MAXLEN DB 12 ACTLEN DB ? OPFLD DB 12 DUP(?) PRMPT DB 0DH, 0AH, 'Enter single-digit integers: $' RSLT DB 0DH, 0AH, 'Occurs = times $' ; .CODE BEGIN PROC FAR ;set up pgm MOV MOV LEA ds AX, @DATA DS, AX DI, RSLT + 11 ;read prompt MOV AH, 09H LEA DX, PRMPT INT 21H ;addr of data seg -> ax ;put addr in ds ;put addr of rslt in di ;display string ;addr of prmpt -> dx ;dos interrupt ;keyboard rtn to enter characters MOV AH, 0AH ;buffered keyboard input LEA DX, PARLST ;load addr of parlst INT 21H ;dos interrupt //continued on next page 770 Appendix B Answers to Select Problems ;keyboard rtn to enter characters MOV AH, 0AH ;buffered keyboard input LEA DX, PARLST ;load addr of parlst INT 21H ;dos interrupt ; MOV DL, OPFLD ;number to compare -> dl LEA SI, OPFLD + ;put addr of list in si MOV CX, ;qty of #s to comp -> cx MOV AH, 30H ;init # of occurrences LP1: MOV CMP JE INC LOOP JMP INC_COUNT: INC INC LOOP AL, [SI] DL, AL INC_COUNT SI LP1 MOVE_COUNT ;move number to al ;compare dl to al ;if equal, jump ;point to next number ;loop to compare next # ;comparison is finished AH SI LP1 ;incr the occurrences ;point to next number ;if cx != 0, ;compare next number MOVE_COUNT: MOV [DI], AH ;# of occurrences ;-> result area ; ;display result MOV AH, 09H ;display string LEA DX, RSLT ;put addr of rslt in dx INT 21H ;dos interrupt BEGIN ENDP END BEGIN Enter single-digit integers; 612634656 Occurs = times -Enter single-digit integers; 123151611 Occurs = times -Enter single-digit integers; 912345679 Occurs = times -Enter single-digit integers; 712345689 Occurs = times -//continued on next page Chapter 17 Additional Programming Examples 771 -Enter single-digit integers; 222222222 Occurs = times -Enter single-digit integers; 412344544 Occurs = times 17.6 Write an assembly language module embedded in a C program to obtain the sum of cubes for the numbers through Display the resulting sum of cubes //sum_of_cubes7.cpp //obtain the sum of cubes for numbers through #include "stdafx.h" int main (void) { //define variables unsigned char n, sum; n = 1; sum = 1; //switch to assembly _asm { // -LP1: INC n MOV CX, MOV AL, n LP2: MUL LOOP ADD CMP JB n LP2 sum, AL n, LP1 } //print sum printf ("Sum of cubes through = %d\n\n", sum); return 0; } Sum of cubes through = 225 Press any key to continue _ 772 Appendix B 17.9 Write an assembly language module embedded in a C program to evaluate the expression shown below for Y using a floating-point number for the variable X The range for X is –3.0 ≤ X ≤ +3.0 Enter several numbers for X and display the corresponding results Answers to Select Problems Y = X3 – 10 X2 + 20 X + 30 //eval_expr8.cpp //evaluate the following expression for Y using a //floating-point number for the variable X: //Y = X^3 -10X^2 + 20X + 30, where X has a //range of -3.0 ST(0) //X^3 -> ST(0) //ST(0) (X^3) //-> flp_rslt_x3 //continued on next page Chapter 17 Additional Programming Examples //calculate 10X^2 FLD flp_x FMUL flp_x FIMUL ten FST flp_rslt_10x2 //calculate 20X FLD flp_x FIMUL twenty FST flp_rslt_20x //calculate Y FLD FSUB FADD //X -> ST(0) //X^2 -> ST(0) //(10) x X^2 -> ST(0) //ST(0) (10X^2) //-> flp_rslt_10x2 //X -> ST(0) //(20) x X -> ST(0) //ST(0) (20X) //-> flp_rslt_20x flp_rslt_x3 flp_rslt_10x2 //(X^3) -> ST(0) //(X^3) - (10X^2) //-> ST(0) flp_rslt_20x //(X^3) - (10X^2) //+ (20X) -> ST(0) FIADD thirty //(X^3) - (10X^2) //+ (20X) + (30) //-> ST(0) FST //ST(0) (Y) //-> flp_rslt_y flp_rslt_y 773 } //printf results printf ("\nResult X^3 = %f\n\n", flp_rslt_x3); printf ("\nResult 10X^2 = %f\n\n", flp_rslt_10x2); printf ("\nResult 20X = %f\n\n", flp_rslt_20x); printf ("\nResult Y = %f\n\n", flp_rslt_y); return 0; } 774 Appendix B Answers to Select Problems Enter a floating-point number for x: –1.0 Result X^3 = –1.000000 Result 10X^2 = 10.000000 Result 20X = –20.000000 Result Y = –1.000000 Press any key to continue _ Enter a floating-point number for x: –2.0 Result X^3 = –8.000000 Result 10X^2 = 40.000000 Result 20X = –40.000000 Result Y = –58.000000 Press any key to continue _ Enter a floating-point number for x: –3.0 Result X^3 = –27.000000 Result 10X^2 = 90.000000 Result 20X = –60.000000 Result Y = –147.000000 Press any key to continue _ //continued on next page Chapter 17 Additional Programming Examples Enter a floating-point number for x: +1.0 Result X^3 = 1.000000 Result 10X^2 = 10.000000 Result 20X = 20.000000 Result Y = 41.000000 Press any key to continue _ Enter a floating-point number for x: +2.0 Result X^3 = 8.000000 Result 10X^2 = 40.000000 Result 20X = 40.000000 Result Y = 38.000000 Press any key to continue _ Enter a floating-point number for x: +3.0 Result X^3 = 27.000000 Result 10X^2 = 90.000000 Result 20X = 60.000000 Result Y = 27.000000 Press any key to continue _ //continued on next page 775 776 Appendix B Answers to Select Problems Enter a floating-point number for x: -1.125 Result X^3 = -1.423828 Result 10X^2 = 12.656250 Result 20X = -22.500000 Result Y = -6.580078 Press any key to continue _ Enter a floating-point number for x: +2.75 Result X^3 = 20.796875 Result 10X^2 = 75.625000 Result 20X = 55.000000 Result Y = 30.171875 Press any key to continue _ 17.12 Given the program segment shown below, obtain the results for the following floating-point numbers: flp_num1 = 125.0, flp_num2 = 245.0 flp_num1 = 650.0, flp_num2 = 375.0 flp_num1 = 755.125, flp_num2 = 575.150 FLD FSQRT FLD FSQRT FSUBP FST flp_num1 flp_num2 ST(1), ST(0) rslt //continued on next page Chapter 17 Additional Programming Examples 17.14 flp_num1 = 125.0, Result = -4.472136 flp_num2 = 245.0 flp_num1 = 650.0, Result = 6.130181 flp_num2 = 375.0 flp_num1 = 755.125, Result = 3.497252 flp_num2 = 575.150 777 Write an assembly language module embedded in a C program to calculate the tangent of the following angles using the sine and cosine of the angles: 30°, 45°, and 60° //tan.cpp //calculate the tangent of the following angles: //30 deg, 45 deg, and 60 deg //using the sine and cosine of the angles //tangent = sine/cosine #include "stdafx.h" int main (void) { //define angles in radians double angle_30, angle_45, angle_60, rslt_30, rslt_45, rslt_60; angle_30 = 0.523598775; //30 deg must be //in radians angle_45 = 0.785398163; //45 deg must be //in radians angle_60 = 1.047197551; //60 deg must be //in radians //continued on next page 778 Appendix B Answers to Select Problems //switch to assembly _asm { //calculate the tangent of 30 degrees FLD angle_30 //30 deg -> ST(0) FSIN //sine 30 -> ST(0) FLD angle_30 FCOS FDIVP ST(1), ST(0) FST rslt_30 //30 deg -> ST(0) //sine 30 -> ST(1) //cos 30 -> ST(0) //sine 30 / cos 30 //-> ST(1), pop //sine 30 / cos 30 //-> ST(0) //store tangent 30 //calculate the tangent of 45 degrees FLD angle_45 //45 deg -> ST(0) FSIN //sine 45 -> ST(0) FLD angle_45 FCOS FDIVP ST(1), ST(0) FST rslt_45 //45 deg -> ST(0) //sine 45 -> ST(1) //cos 45 -> ST(0) //sine 45 / cos 45 //-> ST(1), pop //sine 45 / cos 45 //-> ST(0) //store tangent 45 //calculate the tangent of 60 degrees FLD angle_60 //60 deg -> ST(0) FSIN //sine 60 -> ST(0) FLD angle_60 FCOS FDIVP ST(1), ST(0) FST rslt_60 //60 deg -> ST(0) //sine 60 -> ST(1) //cos 60 -> ST(0) //sine 60 / cos 60 //-> ST(1), pop //sine 60 / cos 60 //-> ST(0) //store tangent 60 } //continued on next page Chapter 17 Additional Programming Examples //display results printf ("Tangent_30 = %f\n\n", rslt_30); printf ("Tangent_45 = %f\n\n", rslt_45); printf ("Tangent_60 = %f\n\n", rslt_60); return 0; } Tangent 30 = 0.577350 Tangent 45 = 1.000000 Tangent 60 = 1.732051 Press any key to continue _ 779 This page intentionally left blank This page intentionally left blank Assembly language programming requires knowledge of number representations, as well as the architecture of the computer on which the language is being used After covering the binary, octal, decimal, and hexadecimal number systems, the book presents the general architecture of the X86 microprocessor, individual addressing modes, stack operations, procedures, arrays, macros, and input/output operations It highlights the most commonly used X86 assembly language instructions, including data transfer, branching and looping, logic, shift and rotate, and string instructions, as well as fixed-point, binary-coded decimal (BCD), and floating-point arithmetic instructions Written for students in computer science and electrical, computer, and software engineering, the book assumes a basic background in C programming, digital logic design, and computer architecture Designed as a tutorial, this comprehensive and self-contained text offers a solid foundation in assembly language for anyone working with the design of digital hardware an informa business www.taylorandfrancisgroup.com 6000 Broken Sound Parkway, NW Suite 300, Boca Raton, FL 33487 711 Third Avenue New York, NY 10017 Park Square, Milton Park Abingdon, Oxon OX14 4RN, UK K16377 X86 Assembly Language and C Fundamentals The predominant language used in embedded microprocessors, assembly language lets you write programs that are typically faster and more compact than programs written in a highlevel language and provide greater control over the program applications Focusing on the languages used in X86 microprocessors, X86 Assembly Language and C Fundamentals explains how to write programs in the X86 assembly language, the C programming language, and X86 assembly language modules embedded in a C program A wealth of program design examples, including the complete code and outputs, help you grasp the concepts more easily Where needed, the book also details the theory behind the design Cavanagh Computer Science & Engineering X86 Assembly C Language and Fundamentals Joseph Cavanagh ISBN: 978-1-4665-6824-2 90000 781466 568242 www.crcpress.com K16377 cvr mech.indd 11/15/12 10:15 AM [...]... Appendix A ASCII Character Codes 681 Appendix B Answers to Select Problems 683 Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Index Number Systems and Number Representations X86 Processor Architecture Addressing Modes C Programming Fundamentals. .. topics that are covered in the examples include logic instructions, bit test instructions, compare instructions, unconditional and conditional jump instructions, unconditional and conditional loop instructions, fixed-point instructions, floating-point instructions, string instructions, and arrays Appendix A lists the American Standard Code for Information Interchange (ASCII) codes for hexadecimal characters... types of exchange instructions are discussed, which exchange the contents of a source and destination location The chapter also presents translate instructions, which change an operand into a different operand in order to translate from one code to another code Chapter 6 presents branching and looping instructions as used in the X86 assembly language These instructions transfer control to a section of... operands in the register stack and sets the condition codes Both versions can also xxii Preface compare operands, set the condition codes, then pop the stack Another version compares operands, sets the condition codes, then pops the stack twice There are also different versions that compare integer operands This chapter also contains instructions that operate on trigonometric functions, such as sine, cosine,... binary, octal, binary-coded octal, decimal, binary-coded decimal, hexadecimal, and binary-coded hexadecimal The chapter also presents the number representations of sign magnitude, diminished-radix complement, and radix complement Chapter 2 presents the generic architecture of processors and how the architecture corresponds more appropriately to the X86 architecture execution environment, including the different... languages which use compilers to accomplish the transformation Assembly languages consist of mnemonic codes, which are similar to English words, making the program easy to read For example, the MOV instruction moves data from a source location to a destination location; the XCHG instruction exchanges the contents of a source location and a destination location; and the logical AND instruction performs... decimal adjust AL after addition instruction, which adjusts the sum of two packed BCD integers to generate a packed BCD result; and the ASCII adjust AL after subtraction instruction, which adjusts the result of a subtraction of two unpacked BCD operands Preface xxi Other BCD operations include the decimal adjust AL after subtraction instruction, which adjusts the result of a subtraction of two packed... digital logic, registers, and stacks In order to thoroughly understand assembly language, it is necessary to be familiar with the architecture of the computer on which the language is being used For the X86 assembly language, this implies the Intel and Intel-like microprocessors Programs written in assembly language are usually faster and more compact than programs written in a high-level language and provide... PREFACE Although assembly language is not as prevalent as a high-level language, such as C or an object-oriented language like C+ +, it is the predominant language used in embedded microprocessors A course in a high-level language, such as C, usually precedes a course in assembly language Assembly language programming requires a knowledge of number representations, such as fixed-point, decimal, and floating-point;... cosine, and combined sine and cosine, which calculates both functions There is also a partial tangent instruction, which calculates the tangent of the source operand in a stack register, then pushes a value of +1.0 onto the stack The partial arctangent instruction is also included, which is the inverse tangent function There are several additional floating-point instructions that perform basic arithmetic ... permission to photocopy or use material electronically from this work, please access www.copyright.com (http:// www.copyright.com/) or contact the Copyright Clearance Center, Inc (CCC), 222 Rosewood.. .X86 Assembly Language and C Fundamentals This page intentionally left blank X86 Assembly Language and C Fundamentals Joseph Cavanagh Santa Clara University, Santa Clara, California CRC Press... Appendix A ASCII Character Codes 681 Appendix B Answers to Select Problems 683 Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter 10 Chapter 11 Chapter 12 Chapter

Ngày đăng: 19/11/2015, 15:06

Từ khóa liên quan

Mục lục

  • Front Cover

  • Dedication

  • Contents

  • Preface

  • Chapter 1: Number Systems and Number Representations

  • Chapter 2: X86 Processor Architecture

  • Chapter 3: Addressing Modes

  • Chapter 4: C Programming Fundamentals

  • Chapter 5: Data Transfer Instructions

  • Chapter 6: Branching and Looping Instructions

  • Chapter 7: Stack Operations

  • Chapter 8: Logical, Bit, Shift, and Rotate Instructions

  • Chapter 9: Fixed-Point Arithmetic Instructions

  • Chapter 10: Binary-Coded Decimal Arithmetic Instructions

  • Chapter 11: Floating-Point Arithmetic Instructions

  • Chapter 12: Procedures

  • Chapter 13: String Instructions

  • Chapter 14: Arrays

  • Chapter 15: Macros

  • Chapter 16: Interrupts and Input/Output Operations

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan