SlideShare a Scribd company logo
1 of 33
What is Assembly Language?
Introduction to the GNU/Linux
assembler and linker
for Intel Pentium processors
High-Level Language
• Most programming nowdays is done using
so-called “high-level” languages (such as
FORTRAN, BASIC, COBOL, PASCAL, C,
C++, JAVA, SCHEME, Lisp, ADA, etc.)
• These languages deliberately “hide” from
a programmer many details concerning
HOW his problem actually will be solved
by the underlying computing machinery
The BASIC language
• Some languages allow programmers to
forget about the computer completely!
• The language can express a computing
problem with a few words of English, plus
formulas familiar from high-school algebra
• EXAMPLE PROBLEM: Compute 4 plus 5
The example in BASIC
1 LET X = 4
2 LET Y = 5
3 LET Z = X + Y
4 PRINT X, “+”, Y, “=“, Z
5 END
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 x = 4, y = 5; // initialized variables
int z; // unitialized variable
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:
10100001 10111100 10010011 00000100
00001000 00000011 00000101 11000000
10010011 00000100 00001000 10100011
11000000 10010100 00000100 00001000
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
Processing
Unit
Main
Memory
I/O
device
I/O
device
I/O
device
I/O
device
system bus
Pentium’s visible “registers”
• Four general-purpose registers:
eax, ebx, ecx, edx
• Four memory-addressing registers:
esp, ebp, esi, edi
• Six memory-segment registers:
cs, ds, es, fs, gs, ss
• An instruction-pointer and a flags register:
eip, eflags
The sixteen x86 registers
EAX ESP
EBX EBP
ECX ESI
EDX EDI
EIP EFLAGS
CS DS ES FS GS SS
Intel Pentium processor
The “Fetch-Execute” Cycle
ESP
EIP
Program
Instructions
(TEXT)
Program
Variables
(DATA)
Temporary
Storage
(STACK)
main memory
central processor
EAX
EAX
EAX
EAX
the system bus
Define symbolic constants
.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)
# comment: request kernel assistance
movl $sys_write, %eax
movl $device_id, %ebx
movl $buf, %ecx
movl n, %edx
int $0x80
‘text’ section (concluded)
# comment: request kernel assistance
movl $sys_exit, %eax
movl $0, %ebx
int $0x80
# comment: make label visible to linker
.global _start
.end
program translation steps
program
source
module
demo.s
program
object
module
assembly
demo.o
the
executable
program
object module library
object module library
other object modules
linking
demo
The GNU Assembler and Linker
• With Linux you get free software tools for
compiling your own computer programs
• An assembler (named ‘as’): it translates
assembly language (called the ‘source code’)
into machine language (called the ‘object code’)
$ as demo.s -o demo.o
• A linker (named ‘ld’): it combines ‘object’ files
with function libraries (if you know which ones)
How a program is loaded
stack
.text
.data
.bss
Runtime libraries
Kernel’s code and data
program instructions
initialized variables
uninitialized variables
Main memory
0x00000000
0xFFFFFFFF
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/
• Execute this compiled applocation using:
$ ./demo1
In-class exercise #2
• Download the two source-files needed for our
‘demo2’ application (i.e., ‘demo2.s’ and
‘sprintf.s’), and assemble them using:
$ as demo2.s -o demo2.o
$ as sprintf.s -o sprintf.o
• Link them using:
$ ld demo2.o sprintf.o -o demo2
• And execute this application using: $ ./demo2
In-class exercise #3
• Use your favorite text-editor (e.g., ‘vi’) to
modify the ‘demo2.s’ source-file, by using
different initialization-values for x and y
• Reassemble your modified ‘demo2.s’ file,
and re-link it with the ‘sprintf.o’ object-file
• Run the modified ‘demo2’ application, and
see if it prints out a result that is correct
In-class exercise #4
• Download the ‘ljpages.cpp’ system-utility
from our class website and compile it:
$ g++ ljpages.cpp –o ljpages
• Execute this utility-program to print your
modified assembly language source-file:
$ ./ljpages demo2.s
• Write your name on the printed hardcopy
and turn it in to your course instructor
Summary of the exercises
Download and compile a high-level program
Assemble and Link a low-level program
Edit and recompile an assembly program
Print out and turn in your hardcopy

More Related Content

Similar to Assembly Langauge Assembly Langauge Assembly Langauge

Assembly language
Assembly languageAssembly language
Assembly languagegaurav jain
 
Embedded programming Embedded programming (1).pptx
Embedded programming Embedded programming (1).pptxEmbedded programming Embedded programming (1).pptx
Embedded programming Embedded programming (1).pptxlematadese670
 
Introduction to computers and programming languages
Introduction to computers and programming languages Introduction to computers and programming languages
Introduction to computers and programming languages binoysatheesh
 
Unit 1 computer concepts
Unit 1   computer conceptsUnit 1   computer concepts
Unit 1 computer conceptsMithun DSouza
 
Introduction_to_Programming.pptx
Introduction_to_Programming.pptxIntroduction_to_Programming.pptx
Introduction_to_Programming.pptxPmarkNorcio
 
computer languages
computer languagescomputer languages
computer languagesRajendran
 
X-CS-8.0 Programming in C Language 2022-2023.pdf
X-CS-8.0 Programming in C Language 2022-2023.pdfX-CS-8.0 Programming in C Language 2022-2023.pdf
X-CS-8.0 Programming in C Language 2022-2023.pdfAlefya1
 
Week 08_Basics of Compiler Construction.pdf
Week 08_Basics of Compiler Construction.pdfWeek 08_Basics of Compiler Construction.pdf
Week 08_Basics of Compiler Construction.pdfAnonymousQ3EMYoWNS
 
Introduction To Computer and Java
Introduction To Computer and JavaIntroduction To Computer and Java
Introduction To Computer and JavaPRN USM
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & LanguagesGaditek
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & LanguagesGaditek
 
Programming Fundamentals and Programming Languages Concepts
Programming Fundamentals and Programming Languages ConceptsProgramming Fundamentals and Programming Languages Concepts
Programming Fundamentals and Programming Languages Conceptsimtiazalijoono
 
C++ programming languages lectures
C++ programming languages lectures C++ programming languages lectures
C++ programming languages lectures jabirMemon
 
Chapter 1 Introduction to C .pptx
Chapter 1 Introduction to C .pptxChapter 1 Introduction to C .pptx
Chapter 1 Introduction to C .pptxAbdalla536859
 
introductiontocomputerprogramming.pptx
introductiontocomputerprogramming.pptxintroductiontocomputerprogramming.pptx
introductiontocomputerprogramming.pptxHazardRhenz1
 

Similar to Assembly Langauge Assembly Langauge Assembly Langauge (20)

Assembly language
Assembly languageAssembly language
Assembly language
 
Embedded programming Embedded programming (1).pptx
Embedded programming Embedded programming (1).pptxEmbedded programming Embedded programming (1).pptx
Embedded programming Embedded programming (1).pptx
 
Introduction to computers and programming languages
Introduction to computers and programming languages Introduction to computers and programming languages
Introduction to computers and programming languages
 
Unit 1 computer concepts
Unit 1   computer conceptsUnit 1   computer concepts
Unit 1 computer concepts
 
Introduction_to_Programming.pptx
Introduction_to_Programming.pptxIntroduction_to_Programming.pptx
Introduction_to_Programming.pptx
 
computer languages
computer languagescomputer languages
computer languages
 
Compilers.pptx
Compilers.pptxCompilers.pptx
Compilers.pptx
 
Ic lecture8
Ic lecture8 Ic lecture8
Ic lecture8
 
X-CS-8.0 Programming in C Language 2022-2023.pdf
X-CS-8.0 Programming in C Language 2022-2023.pdfX-CS-8.0 Programming in C Language 2022-2023.pdf
X-CS-8.0 Programming in C Language 2022-2023.pdf
 
Week 08_Basics of Compiler Construction.pdf
Week 08_Basics of Compiler Construction.pdfWeek 08_Basics of Compiler Construction.pdf
Week 08_Basics of Compiler Construction.pdf
 
CISY 105 Chapter 1
CISY 105 Chapter 1CISY 105 Chapter 1
CISY 105 Chapter 1
 
Plc part 1
Plc part 1Plc part 1
Plc part 1
 
Introduction To Computer and Java
Introduction To Computer and JavaIntroduction To Computer and Java
Introduction To Computer and Java
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Programming Paradigm & Languages
Programming Paradigm & LanguagesProgramming Paradigm & Languages
Programming Paradigm & Languages
 
Programming Fundamentals and Programming Languages Concepts
Programming Fundamentals and Programming Languages ConceptsProgramming Fundamentals and Programming Languages Concepts
Programming Fundamentals and Programming Languages Concepts
 
01CHAP_1.PPT
01CHAP_1.PPT01CHAP_1.PPT
01CHAP_1.PPT
 
C++ programming languages lectures
C++ programming languages lectures C++ programming languages lectures
C++ programming languages lectures
 
Chapter 1 Introduction to C .pptx
Chapter 1 Introduction to C .pptxChapter 1 Introduction to C .pptx
Chapter 1 Introduction to C .pptx
 
introductiontocomputerprogramming.pptx
introductiontocomputerprogramming.pptxintroductiontocomputerprogramming.pptx
introductiontocomputerprogramming.pptx
 

Recently uploaded

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 

Recently uploaded (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 

Assembly Langauge Assembly Langauge Assembly Langauge

  • 1. What is Assembly Language? Introduction to the GNU/Linux assembler and linker for Intel Pentium processors
  • 2. High-Level Language • Most programming nowdays is done using so-called “high-level” languages (such as FORTRAN, BASIC, COBOL, PASCAL, C, C++, JAVA, SCHEME, Lisp, ADA, etc.) • These languages deliberately “hide” from a programmer many details concerning HOW his problem actually will be solved by the underlying computing machinery
  • 3. The BASIC language • Some languages allow programmers to forget about the computer completely! • The language can express a computing problem with a few words of English, plus formulas familiar from high-school algebra • EXAMPLE PROBLEM: Compute 4 plus 5
  • 4. The example in BASIC 1 LET X = 4 2 LET Y = 5 3 LET Z = X + Y 4 PRINT X, “+”, Y, “=“, Z 5 END Output: 4 + 5 = 9
  • 5. 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
  • 6. Same example: rewritten in C #include <stdio.h> // needed for printf() int x = 4, y = 5; // initialized variables int z; // unitialized variable int main() { z = x + y; printf( “%d + %d = %d n”, x, y, z ); }
  • 7. “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
  • 8. 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
  • 9. 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
  • 10. Example of machine-language Here’s what a program-fragment looks like: 10100001 10111100 10010011 00000100 00001000 00000011 00000101 11000000 10010011 00000100 00001000 10100011 11000000 10010100 00000100 00001000 It means: z = x + y;
  • 11. 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!
  • 12. 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
  • 14. Pentium’s visible “registers” • Four general-purpose registers: eax, ebx, ecx, edx • Four memory-addressing registers: esp, ebp, esi, edi • Six memory-segment registers: cs, ds, es, fs, gs, ss • An instruction-pointer and a flags register: eip, eflags
  • 15. The sixteen x86 registers EAX ESP EBX EBP ECX ESI EDX EDI EIP EFLAGS CS DS ES FS GS SS Intel Pentium processor
  • 17. Define symbolic constants .equ device_id, 1 .equ sys_write, 4 .equ sys_exit, 1
  • 18. our program’s ‘data’ section .section .data x: .int 4 y: .int 5 fmt: .asciz “%d + %d = %d n”
  • 19. Our program’s ‘bss’ section .section .bss z: .int 0 n: .int 0 buf: .space 80
  • 20. our program’s ‘text’ section .section .text _start: # comment: assign z = x + y movl x, %eax addl y, %eax movl %eax, z
  • 21. ‘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
  • 22. ‘text’ section (continued) # comment: request kernel assistance movl $sys_write, %eax movl $device_id, %ebx movl $buf, %ecx movl n, %edx int $0x80
  • 23. ‘text’ section (concluded) # comment: request kernel assistance movl $sys_exit, %eax movl $0, %ebx int $0x80 # comment: make label visible to linker .global _start .end
  • 25. The GNU Assembler and Linker • With Linux you get free software tools for compiling your own computer programs • An assembler (named ‘as’): it translates assembly language (called the ‘source code’) into machine language (called the ‘object code’) $ as demo.s -o demo.o • A linker (named ‘ld’): it combines ‘object’ files with function libraries (if you know which ones)
  • 26. How a program is loaded stack .text .data .bss Runtime libraries Kernel’s code and data program instructions initialized variables uninitialized variables Main memory 0x00000000 0xFFFFFFFF
  • 27. 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)
  • 28. 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
  • 29. 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/ • Execute this compiled applocation using: $ ./demo1
  • 30. In-class exercise #2 • Download the two source-files needed for our ‘demo2’ application (i.e., ‘demo2.s’ and ‘sprintf.s’), and assemble them using: $ as demo2.s -o demo2.o $ as sprintf.s -o sprintf.o • Link them using: $ ld demo2.o sprintf.o -o demo2 • And execute this application using: $ ./demo2
  • 31. In-class exercise #3 • Use your favorite text-editor (e.g., ‘vi’) to modify the ‘demo2.s’ source-file, by using different initialization-values for x and y • Reassemble your modified ‘demo2.s’ file, and re-link it with the ‘sprintf.o’ object-file • Run the modified ‘demo2’ application, and see if it prints out a result that is correct
  • 32. In-class exercise #4 • Download the ‘ljpages.cpp’ system-utility from our class website and compile it: $ g++ ljpages.cpp –o ljpages • Execute this utility-program to print your modified assembly language source-file: $ ./ljpages demo2.s • Write your name on the printed hardcopy and turn it in to your course instructor
  • 33. Summary of the exercises Download and compile a high-level program Assemble and Link a low-level program Edit and recompile an assembly program Print out and turn in your hardcopy