CS162B: Assembly and C Jacob T. Chan. Objectives ▪ System calls ▪ Relation of System calls to Assembly and C ▪ Special System Calls (exit, write, print,

Slides:



Advertisements
Similar presentations
Dr. Ken Hoganson, © August 2014 Programming in R COURSE NOTES 2 Hoganson Language Translation.
Advertisements

1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
CS162B: POSIX Threads Jacob Chan. Objectives ▪ Review on fork() and exec() – Some issues on forking and exec-ing ▪ POSIX Threads ▪ Lab 8.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
PC hardware and x86 3/3/08 Frans Kaashoek MIT
CS-502 Fall 2006Project 1, Fork1 Programming Project 1 – Fork.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Advanced OS Chapter 3p2 Sections 3.4 / 3.5. Interrupts These enable software to respond to signals from hardware. The set of instructions to be executed.
X86 ISA Compiler Baojian Hua Front End source code abstract syntax tree lexical analyzer parser tokens IR semantic analyzer.
CSE 451 Section 4 Project 2 Design Considerations.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 2: Operating-System Structures Modified from the text book.
Introduction to Linux and Shell Scripting Jacob Chan.
Computer Science 210 Computer Organization Modular Decomposition Making a Library Separate Compilation.
1 - buttons Click “Step Forward” to execute one line of the program. Click “Reset” to start over. “Play,” “Stop,” and “Step Back” are disabled in this.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
CS110/CS119 Introduction to Computing (Java)
Introduction to InfoSec – Recitation 2 Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)
6.828: PC hardware and x86 Frans Kaashoek
CS162B: Semaphores (and Shared Memory) Jacob T. Chan.
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
CS162B: Forking Jacob T. Chan. Fork  Forks are:  Implement with two or more prongs that is used for taking up or digging  Division into branches or.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Programming With C.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 6 System Calls OS System.
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
File ▪ File – Unit of logical storage – Aid in manipulating exact sector of file data ▪ Abstract view of secondary physical storage devices ▪ Without files.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
Operating Systems Lecture November 2015© Copyright Virtual University of Pakistan 2 Agenda for Today Review of previous lecture Hardware (I/O, memory,
CNIT 127: Exploit Development Ch 3: Shellcode. Topics Protection rings Syscalls Shellcode nasm Assembler ld GNU Linker objdump to see contents of object.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CSE 351 GDB Introduction. Lab 1 Status? How is Lab 1 going? I’ll be available at the end of class to answer questions There are office hours later today.
Operating Systems Process Creation
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
OS Labs 2/25/08 Frans Kaashoek MIT
I/O Software CS 537 – Introduction to Operating Systems.
1 Project 5: Leap Years. 222 Leap Years Write a program that reads an integer value from the user representing a year and determines if the year is a.
1 Building a program in C: Preprocessor, Compilation and Linkage.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Practical Session 3.
History of C and basics of Programming
Computer Architecture and Assembly Language
User-Written Functions
Credits and Disclaimers
Computer Science 210 Computer Organization
Protection of System Resources
Homework Reading Machine Projects Labs PAL, pp ,
Practical example of C programming
Software Development with uMPS
Computer Science 210 Computer Organization
Programmazione I a.a. 2017/2018.
Assembly Language Programming II: C Compiler Calling Sequences
Computer Science 210 Computer Organization
Discussions on HW2 Objectives
Chapter 9 TRAP Routines and Subroutines
System Calls David Ferry CSCI 3500 – Operating Systems
Chapter 9 TRAP Routines and Subroutines
Discussions on HW2 Objectives
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Presentation transcript:

CS162B: Assembly and C Jacob T. Chan

Objectives ▪ System calls ▪ Relation of System calls to Assembly and C ▪ Special System Calls (exit, write, print, etc.) ▪ Lab 5

Processing… ▪ Some programs take too long to run – Running from console terminal will prevent you from running other programs in the same terminal (until program exits) – Issue when system complains for running too much consoles running ▪ Program vs process? – One solution: put & ▪ Makes program run in background ▪ BUT it does not make the program a daemon (more on this later on)

Exit Code ▪ In C/C++, main() method returns 0 (unless there was something wrong) – Same goes for Java’s System.exit() that takes in an int as an argument ▪ This int is usually 0 (unless specified otherwise due to errors) ▪ Exit code or return value is tested by whatever called the program (either by main() or by exit()) – Zero (0) = normal program termination – Non-zero = abnormal termination of program ▪ Cause: ERRORS ▪ Return value may give a hint of what type of error it is

System Calls ▪ Layer between user and kernel ▪ Lowest interaction level with O/S ▪ Entry point to the kernel – Should be provided by O/S ▪ Everything else is built on top of the system call ▪ Normally uses C/C++ – Not Java (because system calls only relay signals to JVM) – Java system calls result in OVERHEAD – And besides, JVM is a VIRTUAL MACHINE

Categories of System Calls ▪ Process Control ▪ File Manipulation ▪ Signals (obsolete; system calls rely on signals) ▪ Device Management ▪ Information Maintenance ▪ Communications

Categories of System Calls ▪ System calls may fall on more than one category – chmod is both file manipulation and system maintenance – abort is process control and signal system call

In relation to Assembly and C… ▪ Function calls behave similarly for Assembly code – CS152a format of function calls is same in Assembly – GNU Assembler format will be used for this class – AT&T Syntax will be followed – Show assembly code in C programs – We will assume a 32-bit architecture (not 64-bit) ▪ Might not work. Why?

GAS ▪ GNU Assembler files end with.S ▪ Programs in C are converted to assembly then to binary during compiling

Registers ▪ ESP, EAX, EDP, etc. are the registers – Remember R0 to R31? ▪ What is the register name for the stack pointer? The base pointer? ▪ GAS code follows Beta assembly code (BSIM)

Defining Functions in GAS... ▪ This is just crash course Assembly (it’s gonna take a separate semester if so) ▪ Defining a function myFunc.text.globl myFunc.type myFunc: # code follows # this is a comment

System calls in GAS ▪ Some functions provide kernel-mode only functionality – Just like sudo ▪ To obtain such features, function must issue signal to O/S via interrupt ▪ In Linux, this is achieved by typing int $0x80

Example: Write function ▪ Check out the mySysWrite.S (uploaded in Moodle) ▪ Then add it to C code (and use it like a regular function) extern int myFunc(int fildes,const void *buf, int nbyte); ▪ Compiling the program gcc -o demoProg myProg.c myFunc.S

Required: Knowledge ▪ In making system calls, you need: – The number code to be inserted in the eax register before calling interrupt – Arguments to be placed in ebx, ecx, etc.

Lab 5: Making Assembly Sys Calls ▪ Implement system calls for opening, closing, reading, writing, and exiting using assembly in C – NOTE: writing is already demonstrated here in C ▪ Write a C program copy.c that makes use of these system calls – This copies specified input file to a specified output file – DO NOT USE the stdio.h library functions for this one (printf/scanf) – Output file is to be named “output.txt” but for input files, assume it’s a.txt file

Lab 5: Making Assembly Sys Calls ▪ Include a Certificate of Authorship, your copy.c, and your.S files (open.S, write.S, read.S, close.S, exit.S) ▪ Filename Convention: CS162B_Lab5_ _ _.tar ▪ Deadline: 3 Days after today