Lecture 5

advertisement
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, @function
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_<Section>_<Surname>_<ID#>.tar
▪ Deadline: 3 Days after today
Download