IA32 programming for Linux Concepts and requirements for writing Linux assembly language programs for Pentium CPUs.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Instruction Set Design
The Assembly Language Level
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Chapter 3 Assembly Language: Part 1. Machine language program (in hex notation) from Chapter 2.
1 Lecture 1  Getting ready to program  Hardware Model  Software Model  Programming Languages  The C Language  Software Engineering  Programming.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
IA32 programming for Linux Concepts and requirements for writing Linux assembly language programs for Pentium CPUs.
What is Assembly Language? Introduction to the GNU/Linux assembler and linker for Intel Pentium processors.
Modules, Hierarchy Charts, and Documentation
IA32 programming for Linux Concepts and requirements for writing Linux assembly language programs for Pentium CPUs.
Guide To UNIX Using Linux Third Edition
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Introduction to C Programming
CS2422 Assembly Language & System Programming September 26, 2006.
Assembly & Machine Languages
Chapter 2 Software Tools and Assembly Language Syntax.
CS102 Introduction to Computer Programming
1 Chapter-01 Introduction to Computers and C++ Programming.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Programming. What is a Program ? Sets of instructions that get the computer to do something Instructions are translated, eventually, to machine language.
1/2002JNM1 Basic Elements of Assembly Language Integer Constants –If no radix is given, the integer is assumed to be decimal. Int 21h  Int 21 –A hexadecimal.
Introduction CS 104: Applied C++ What is Programming? For some given problem: __________ a solution for it -- identify, organize & store the problem's.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Visual Basic.NET BASICS Lesson 4 Mathematical Operators.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Robert Crawford, MBA West Middle School.  Explain how the binary system is used by computers.  Describe how software is written and translated  Summarize.
CSCI 211 Intro Computer Organization –Consists of gates for logic And Or Not –Processor –Memory –I/O interface.
Ass. Prof. Dr Masri Ayob TK 6123 Lecture 13: Assembly Language Level (Level 4)
Execution of an instruction
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Let’s do a code ‘walk-through’ Here we examine the design details for a short, but not trivial, assembly language example.
Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --
Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Computer and Programming. Computer Basics: Outline Hardware and Memory Programs Programming Languages and Compilers.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
8086/8088 Instruction Set, Machine Codes and Addressing Modes.
Embedding Assembly Code in C Programs תרגול 7 שילוב קוד אסמבלי בקוד C.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
Skill Area 311 Part B. Lecture Overview Assembly Code Assembler Format of Assembly Code Advantages Assembly Code Disadvantages Assembly Code High-Level.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Software Engineering Algorithms, Compilers, & Lifecycle.
Engineering Problem Solving With C An Object Based Approach
Assembly Language Ms. V.Anitha AP/CSE SCT
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 3 Machine Language and Assembly Language.
Chapter 3 Machine Language and Assembly Language.
MIPS assembly syntax Comments
BIC 10503: COMPUTER ARCHITECTURE
ECEG-3202 Computer Architecture and Organization
PROGRAMMING FUNDAMENTALS Lecture # 03. Programming Language A Programming language used to write computer programs. Its mean of communication between.
Chapter 6 Programming the basic computer
Presentation transcript:

IA32 programming for Linux Concepts and requirements for writing Linux assembly language programs for Pentium CPUs

A source program’s format Source-file: a pure ASCII-character textfile Is created using a text-editor (such as ‘vi’) You cannot use a ‘word processor’ (why?) Program consists of series of ‘statements’ Each program-statement fits on one line Program-statements all have same layout Design in 1950s was for IBM punch-cards

Statement Layout (1950s) Each ‘statement’ was comprised of four ‘fields’ Fields appear in a prescribed left-to-right order These four fields were named (in order): -- the ‘label’ field -- the ‘opcode’ field -- the ‘operand’ field -- the ‘comment’ field In many cases some fields could be left blank Extreme case (very useful): whole line is blank!

The ‘as’ program The ‘assembler’ is a computer program It accepts a specified text-file as its input It must be able to ‘parse’ each statement It can produce onscreen ‘error messages’ It can generate an ELF-format output file (That file is known as an ‘object module’) It can also generate a ‘listing file’ (optional)

The ‘label’ field A label is a ‘symbol’ followed by a colon (‘:’) The programmer invents his own ‘symbols’ Symbols can use letters and digits, plus a very small number of ‘special’ characters ( ‘.’, ‘_’, ‘$’ ) A ‘symbol’ is allowed to be of arbitrarily length The Linux assembler (‘as’) was designed for translating source-text produced by a high-level language compiler (such as ‘cc’) But humans can also write such files directly

The ‘opcode’ field Opcodes are predefined symbols that are recognized by the GNU assembler There are two categories of ‘opcodes’ (called ‘instructions’ and ‘directives’) ‘Instructions’ represent operations that the CPU is able to perform (e.g., ‘add’, ‘inc’) ‘Directives’ are commands that guide the work of the assembler (e.g., ‘.globl’, ‘.int’)

Instructions vs Directives Each ‘instruction’ gets translated by ‘as’ into a machine-language statement that will be fetched and executed by the CPU when the program runs (i.e., at ‘runtime’) Each ‘directive’ modifies the behavior of the assembler (i.e., at ‘assembly time’) With GNU assembly language, they are easy to distinguish: directives begin with ‘.’

A list of the Pentium opcodes An ‘official’ list of the instruction codes can be found in Intel’s programmer manuals: But it’s three volumes, nearly 1000 pages (it describes ‘everything’ about Pentiums) An ‘unofficial’ list of (most) Intel instruction codes can fit on one sheet, front and back:

The AT&T syntax The GNU assembler uses AT&T syntax (instead of official Intel/Microsoft syntax) so the opcode names differ slightly from names that you will see on those lists: Intel-syntax AT&T-syntax ADD  addb/addw/addl INC  incb/incw/incl CMP  cmpb/cmpw/cmpl

The UNIX culture Linux is intended to be a version of UNIX (so that UNIX-trained users already know Linux) UNIX was developed at AT&T (in early 1970s) and AT&T’s computers were built by DEC, thus UNIX users learned DEC’s assembley language Intel was early ally of DEC’s competitor, IBM, which deliberately used ‘incompatible’ designs Also: an ‘East Coast’ versus ‘West Coast’ thing (California, versus New York and New Jersey)

Bytes, Words, Longwords CPU Instructions usually operate on data-items Only certain sizes of data are supported: BYTE: one byte consists of 8 bits WORD: consists of two bytes (16 bits) LONGWORD: uses four bytes (32 bits) With AT&T’s syntax, an instruction’s name also incorporates its effective data-size (as a suffix) With Intel syntax, data-size usually isn’t explicit, but is inferred by context (i.e., from operands)

The ‘operand’ field Operands can be of several types: -- a CPU register may hold the datum -- a memory location may hold the datum -- an instruction can have ‘built-in’ data -- frequently there are multiple data-items -- and sometimes there are no data-items An instruction’s operands usually are ‘explicit’, but in a few cases they also could be ‘implicit’

Examples of operands Some instruction that have two operands: movl%ebx, %ecx addl$4, %esp Some instructions that have one operand: incl%eax pushl$fmt An instruction that lacks explicit operands: ret

The ‘comment’ field An assembly language program often can be hard for a human being to understand Even a program’s author may not be able to recall his programming idea after awhile So programmer ‘comments’ can be vital A comments begin with the ‘#’ character The assembler disregards all comments (but they will appear in program listings)

‘Directives’ Sometimes called ‘pseudo-instructions’ They tell the assembler what to do The assembler will recognize them Their names begin with a dot (‘.’) Examples:‘.section’, ‘.global’, ‘.int,’ … The names of valid directives appears in the table-of-contents of the GNU manual

New program example Let’s look at a demo program (‘squares.s’) It prints out a mathematical table showing some numbers and their squares But it doesn’t use any multiplications! It uses an algorithm based on algebra: (n+1) 2 - n 2 = n + n + 1 If you already know the square of a given number n, you can get the square of the next number n+1 by just doing additions

A program with a ‘loop’ Here’s our program idea (expressed in C) intnum = 1, val = 1; do{ printf( “ %d %d \n”, num, val ); val += num + num + 1; num += 1; } while ( num <= 20 );

Some new ‘directives’ ‘.equ’ – equates a symbol to a value:.equmax, 20 ‘.globl’ – just an alternative to ‘.global’:.globlmain

Some new ‘instructions’ ‘inc’ – adds one to the specified operand: inclarg ‘cmp’ – compares two specified operands: cmpl$max, arg ‘jle’ – jump (to a specified instruction) if condition ‘less than or equal to’ is true: jleagain

In-class Exercise Can you write a program that prints out a table showing powers of 2 (very useful for computer science students to have handy) Can you see how to do it without using any ‘multiply’ operations – just additions? Hint: study the ‘squares.s’ source-code Then write your own ‘powers.s’ solution Turn in printouts (source and its output)