CS220 Library User's Manual May 17, 2007

Similar documents
Assembly basics CS 2XA3. Term I, 2017/18

Basic Assembly. Ned Nedialkov. McMaster University Canada. SE 3F03 January 2014

Language of x86 processor family

Subprograms: Local Variables

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

Subprograms: Arguments

COMP2421 COMPUTER ORGANZATION. Lab 3

6 Character Classification and Utilities Module (chars.hhf)

Intel assembly language using gcc

Stack, subprograms. procedures and modular programming role of stack while using procedures stack implementation (Pentium)

MIPS Architecture and Assembly Language Overview

Short Notes of CS201

Course Administration

CS201 - Introduction to Programming Glossary By

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Starting to Program in C++ (Basics & I/O)

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

Chapter 11 Introduction to Programming in C

C for C++ Programmers

Work relative to other classes

MIPS and QTSPIM Recap. MIPS Architecture. Cptr280. Dr Curtis Nelson. Cptr280, Autumn

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

Important From Last Time

Sistemi Operativi. Lez. 16 Elementi del linguaggio Assembler AT&T

Linking and Loading. ICS312 - Spring 2010 Machine-Level and Systems Programming. Henri Casanova

SPIM & MIPS Programming

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 5: Procedures. Chapter Overview. The Book's Link Library

Important From Last Time

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Chapter 11 Introduction to Programming in C

Layers of Abstraction CS 3330: C. Compilation Steps. What s in those files? Higher-level language: C. Assembly: X86-64.

MIPS Assembly Language

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Computer Labs: Mixed C and Assembly Programming

Chapter 2 Basic Elements of C++

Introduction Presentation A

Page 1. Today. Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right? Compiler requirements CPP Volatile

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Full file at

printf( Please enter another number: ); scanf( %d, &num2);

Operating Systems. Project #2: System Calls

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

PC Assembly Language. Paul A. Carter

BİL200 TUTORIAL-EXERCISES Objective:

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

How Compiling and Compilers Work

Linkers and Loaders. CS 167 VI 1 Copyright 2008 Thomas W. Doeppner. All rights reserved.

SPIM Instruction Set

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Should you know scanf and printf?

INDEX. Figure I-0. Listing I-0. Table I-0. Symbols.DIRECTIVE (see Assembler directives)? preprocessor operator 3-34

Compilation, Disassembly, and Profiling (in Linux)

Objectives. In this chapter, you will:

PC Assembly Language. Paul A. Carter

mith College Computer Science CSC231 Assembly Week #12 Thanksgiving 2017 Dominique Thiébaut

UNIVERSAL SERIAL INTERFACE

Goals of this Lecture

Computer Architecture and System Software Lecture 06: Assembly Language Programming

Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8

Compila(on, Disassembly, and Profiling

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

Assignment 1: Pipelining Implementation at SPIM Simulator

Simple C Program. Assembly Ouput. Using GCC to produce Assembly. Assembly produced by GCC is easy to recognize:

INTRODUCTION 1 AND REVIEW

Constants and. Lecture 7: Assembly Language Programs. Expressions (cont.) Constants and. Statements. Expressions

Lecture 3: C Programm

Chapter 11 Introduction to Programming in C

LESSON 4. The DATA TYPE char

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Process Layout and Function Calls

Assembly Language for Intel-Based Computers, 4 th Edition

Lecture 4: Build Systems, Tar, Character Strings

Lecture 2: C Programming Basic

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

Advanced C Programming Topics

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

Introduction to Programming (Java) 2/12

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

Lab 5: Input/Output using a Library of Procedures

Chapter 7: User Defined Functions and Stack Mechanics

x86 assembly CS449 Fall 2017

The C++ Language. Arizona State University 1

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSI33 Data Structures

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. A Taste of C. Agenda.

Homework / Exam. Return and Review Exam #1 Reading. Machine Projects. Labs. S&S Extracts , PIC Data Sheet. Start on mp3 (Due Class 19)

Chapter 11 Introduction to Programming in C

Preprocessor Directives

Chapter 11 Introduction to Programming in C

MIPS Processor Overview

Assembler Programming. Lecture 10

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Handout 7, Lex (5/30/2001)

Transcription:

CS220 Library User's Manual May 17, 2007 Introduction The CS220 library contains several routines that simplify input and output from assembly language programs. The included routines are listed in Table 1. Examples Assume we have the following definitions in the data section:.section.data msg1:.asciz Hello world! msg2:.asciz Enter an integer: msg3:.asciz Enter a line of text buff:.fill 80 xval:.int 12345 yval:.int 0 The following code illustrates the use of several of the routines in the library. # Display "Hello world!" message movl $msg1, %eax # Display the value of the xval variable movl xval, %eax call print_int # Prompt for an integer, store in yval variable movl $msg2, %eax call read_int movl %eax, yval call read_nl # Prompt for a line of text, store at location buff # Display the line of text. movl $msg3, %eax movl $buff, %eax call read_line movl $buff, %eax CS220 1 Richardson

Note that, in AT&T syntax, $label is an address while label is a memory reference, so movl xval, %eax would move the value stored at memory location xval into register EAX, while movl $xval, %eax would move the address of xval into register EAX. Notes 1. All of the routines use the C stdio routines for input/output. 2. All of the output routines (print_char, print_int, print_uint, print_int, print_single, print_double, print_str, print_nl) flush the stdout buffer after calling the stdio output routine. Although this is not efficient, it ensures that output appears on the screen immediately. Beginning programmers usually expect output to appear immediately when an output routine is used and troubleshooting efforts via instrumentation (using print statements) can be stymied if this is not the case. 3. The read_int, read_uint, read_single, read_double, and read_str routines skip over any leading whitespace (space, tabs, line feeds, carriage returns, etc) while the read_char and read_line routines do not. However, read_int, read_uint, read_single, read_double, and read_str leave trailing whitespace in the input stream. This can cause unexpected behavior when mixing calls that skip whitespace with those that do not. For example, using read_int to read an integer at the end of a line and then calling read_line will actually read what is left in the line after the integer (perhaps nothing) and not the next line. Use read_nl after the call to read_int and before the call to read_line to read the whitespace (including the line feed) after the integer. The call to read_line will then read the next line. Usage An assembly program to be linked with the library should have the following general form:.globl _asm_main # Initialized data goes in the data segment.section.data # Uninitialized data goes in the bss segment.section.bss # Code goes in the text segment.section.text _asm_main: enter $0, $0 # set up stack frame pusha # save all registers # User-written code goes here. popa # restore all registers movl $0, %eax # return program status in eax leave # restore stack frame ret CS220 2 Richardson

The name of the entry point must be _asm_main and _asm_main must be declared global as shown. This code is in the skeleton.s file that is in the library archive. To link assembly file foobar.s with the library (libcs220.a) invoke g++ as follows: g++ -o foobar foobar.s -LLIBDIR -lcs220 where LIBDIR should be replaced by the path to the directory containing the library (use a single period to represent the current directory). The library includes a C++ main program that calls _asm_main. If you want to link your own C++ main programs with an assembly routine that calls I/O routines in the CS220 library you will need to use the following compilation options: g++ -o foobar -Wl,--defsym -Wl,_asm_main=0 \ -Wl,--allow-multiple-definition foo.cpp bar.s \ -LLIBDIR -lcs220 The options after -Wl, (that's W-ell, not W-one) are passed to the linker and are necessary to prevent link errors due to the multiple definitions of main (and presumably an undefined _asm_main routine). In a Makefile this can be accomplished more neatly using: LDOPTSA=-Wl,--defsym -Wl,_asm_main=0 LDOPTS=$(LDOPTSA) -Wl,--allow-multiple-definition foobar: foo.cpp bar.s g++ -o foobar $(LDOPTS) foo.cpp bar.s -LLIBDIR -lcs220 CS220 3 Richardson

Name Input Register Output Register Description print_char %eax Sends an ASCII character stored in register EAX to standard output. The character should be stored in the lowest byte of the EAX register. The upper three bytes should contain zeroes. print_int %eax Converts a 32 bit signed integer stored in register EAX to ASCII and sends the result to standard output. print_uint %eax Converts a 32 bit unsigned integer stored in register EAX to ASCII and sends the result to the standard output. print_single %st(0) Converts a 32 bit single precision float stored in register ST(0) to ASCII and sends the result to the standard output. print_double %st(0) Converts a 32 bit double precision float stored in register ST(0) to ASCII and sends the result to the standard output. print_str %eax Sends a null-terminated C string (ASCII char array) whose address is in EAX to standard output. print_nl Sends an ASCII line feed character to standard output. read_char %eax Returns a character read from standard input in register EAX. The character is returned in the lowest byte of EAX, the upper three bytes are set to zero. read_int %eax Converts an ASCII representation of an integer read from standard input to a signed 32 bit integer and returns the result in register EAX. Leading read_uint %eax Converts an ASCII representation of a positive integer read from standard input to an unsigned 32 bit integer and returns the result in register EAX. Leading read_single %st(0) Converts an ASCII representation of a float read from standard input to a 32 bit single precision float and returns the result in register ST(0). Leading read_double %st(0) Converts an ASCII representation of a float read from standard input to a 64 bit double precision float and returns the result in register ST(0). Leading read_str %eax Reads a white-space delimited string (a word) from standard input and stores it at the address in register EAX. The string is null terminated. Leading read_line %eax Reads a linefeed terminated line from standard input and stores it at the address in register EAX. The string is null terminated. The linefeed is removed from the input buffer. Reads in a maximum of 255 characters. The buffer should be 256 bytes in length (255 characters plus the trailing null character). The buffer may be shorter, but then buffer overflow may occur if the input line contains more characters than the buffer can hold. read_nl Reads and discards all characters up to and including the next linefeed. str_length %eax %eax Determines the length of a null-terminated C string (ASCII char array) whose address is in EAX. The null-terminating byte is not included in the length value. The length is returned in the EAX register. Table 1: Routines in the CS220 Assembly Library CS220 4 Richardson

Preprocessing and Debugging Assembly source code files that have a.s (uppercase s) suffix are automatically run through the C preprocessor when assembled using either gcc or g++. Source code files that have a.s (lowercase s) are assumed to be pure assembly and may not contain preprocessor directives. The use of preprocessor definitions in assembly can result in programs that are easier to read and maintain. They are particularly useful when using local variables in an assembly subprogram. For example: #define X_INT -4(%ebp) #define Y_DBL -12(%ebp) The X_INT and Y_DBL labels can then be used throughout the assembly source code file. There is a slight problem when trying to debug assembly-with-preprocessor source code. The preprocessor creates a pure assembly source code file in the /tmp directory that is then run through the assembler. When the debugger is started it looks for an assembly source file in the /tmp directory (which has since been deleted) and not the original assembly-with-preprocessor source file. The solution is to either use pure assembly when wanting to use the debugger (use a lowercase s suffix for the assembly source code file) or to break down preprocessing and assembly into two separate stages. I prefer to use the asm suffix on files that contain assembly-with-preprocessor source code. (This prevents problems on case insensitive file systems.) To create an object file with debugging information from a source file named foo.asm then do the following: cpp -P -x assembler-with-cpp foo.asm foo.s g++ -g -c foo.s The file foo.s will then serve as the source code file for the debugger. It should be similar enough to the original foo.asm file so that debugging is not too confusing. CS220 5 Richardson