Lesson 01
Lesson 01
Lesson 01
Output: 4+5=9
The C language
• Other high-level languages do require a small
amount of awareness by the program-author of
how a computation is going to be processed
• For example, that:
- the main program will get “linked” with a
“library” of other special-purpose subroutines
- instructions and data will get placed into
separate sections of the machine’s memory
- variables and constants get treated differently
- data items have specific space requirements
Same example: rewritten in C
#include <stdio.h> // needed for printf()
int main()
{
z = x + y;
printf( “%d + %d = %d \n”, x, y, z );
}
“ends” versus “means”
• Key point: high-level languages let programmers
focus attention on the problem to be solved, and
not spend effort thinking about details of “how” a
particular piece of electrical machiney is going to
carry out the pieces of a desired computation
• Key benefit: their problem gets solved sooner
(because their program can be written faster)
• Programmers don’t have to know very much
about how a digital computer actually works
computer scientist vs. programmer
• But computer scientists DO want to know
how computers actually work:
-- so we can fix computers if they break
-- so we can employ optimum algorithms
-- so we can predict computer behavior
-- so we can devise faster computers
-- so we can build cheaper computers
-- so we can pick one suited to a problem
A machine’s own language
• For understanding how computers work,
we need familiarity with the computer’s
own language (called “machine language”)
• It’s LOW-LEVEL language (very detailed)
• It is specific to a machine’s “architecture”
• It is a language “spoken” using voltages
• Humans represent it with zeros and ones
Example of machine-language
Here’s what a program-fragment looks like:
It means: z = x + y;
Incomprehensible?
• Though possible, it is extremely difficult,
tedious (and error-prone) for humans to
read and write “raw” machine-language
• When unavoidable, a special notation can
help (called hexadecimal representation):
A1 BC 93 04 08
03 05 C0 93 04 08
A3 C0 94 04 08
• But still this looks rather meaningless!
Hence assembly language
• There are two key ideas:
-- mnemonic opcodes: we use abbreviations of
English language words to denote operations
-- symbolic addresses: we invent “meaningful”
names for memory storage locations we need
• These make machine-language understandable
to humans – if they know their machine’s design
• Let’s see our example-program, rewritten using
actual “assembly language” for Intel’s Pentium
Simplified Block Diagram
Central
Main
Processing
Memory
Unit
system bus
EAX ESP
EBX EBP
ECX ESI
EDX EDI
CS DS ES FS GS SS
EIP EFLAGS
Program
EAX
EAX
EAX Variables
EAX (DATA)
Program
EIP Instructions
(TEXT)
.equ device_id, 1
.equ sys_write, 4
.equ sys_exit, 1
our program’s ‘data’ section
.section .data
x: .int 4
y: .int 5
fmt: .asciz “%d + %d = %d \n”
Our program’s ‘bss’ section
.section .bss
z: .int 0
n: .int 0
buf: .space 80
our program’s ‘text’ section
.section .text
_start:
# comment: assign z = x + y
movl x, %eax
addl y, %eax
movl %eax, z
‘text’ section (continued)
# comment: prepare program’s output
pushl z # arg 5
pushl y # arg 4
pushl x # arg 3
pushl $fmt # arg 2
pushl $buf # arg 1
call sprintf # function-call
addl $20, %esp # discard the args
movl %eax, n # save return-value
‘text’ section (continued)
demo.s
demo.o
demo
program assembly program linking
source object
module module the
executable
program
stack
Runtime libraries
0x00000000
Main memory
What must programmer know?
• Needed to use CPU register-names (eax)
• Needed to know space requirements (int)
• Needed to know how stack works (pushl)
• Needed to make symbol global (for linker)
• Needed to understand how to quit (exit)
• And of course how to use system tools:
(e.g., text-editor, assembler, and linker)
Summary
• High-level programming (offers easy and
speedy real-world problem-solving)
• Low-level programming (offers knowledge
and power in utilizing machine capabilities)
• High-level language hides lots of details
• Low-level language reveals the workings
• High-level programs: readily ‘portable’
• Low-level programs: tied to specific CPU
In-class exercise #1
• Download the source-file for ‘demo1’, and
compile it using the GNU C compiler ‘gcc’:
$ gcc demo1.c -o demo1
Website: http://cs.usfca.edu/~cruse/cs210/