What is Assembly Language? - PowerPoint PPT Presentation

About This Presentation
Title:

What is Assembly Language?

Description:

Most programming nowdays is done using so-called 'high-level' languages (such as ... The Python Language Reference manual is handily accessible online: ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 35
Provided by: professora5
Learn more at: https://www.cs.usfca.edu
Category:

less

Transcript and Presenter's Notes

Title: What is Assembly Language?


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
  • LET X 4
  • LET Y 5
  • LET Z X Y
  • PRINT X, , Y, , Z
  • END

Output 4 5 9
5
The Python language
  • It shares some of the best features of the
    earlier BASIC language but improves it by
    incorporating more modern capabilities
  • The Python Language Reference manual is handily
    accessible online
  • lthttp//docs.python.org/reference/gt
  • Lots of tutorials and program examples can be
    quickly discovered using google

6
Example in Python
  • If you type these Python statements into a
    textfile named example, you can execute them in
    a Windows or Linux environment
  • e.g., python example

x 4 y 5 z x y print x, , y, , z
7
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 machines memory
  • - variables and constants get treated
    differently
  • - data items have specific space requirements

8
Same example rewritten in C
  • include ltstdio.hgt // 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 )

9
ends versus means
  • Key point high-level languages let programmers
    focus attention on the problem to be solved, and
    not exspend 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 dont have to know very much about
    how a digital computer actually works

10
computer scientist vs. programmer
  • But computer scientists DO want to know how
    computers actually work
  • -- so we can diagnose computer errors
  • -- 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

11
A machines own language
  • For understanding how computers work, we need
    familiarity with the computers own language
    (called machine language)
  • Its LOW-LEVEL language (very detailed)
  • It is specific to a machines architecture
  • It is a language spoken using voltages
  • Humans represent it with zeros and ones

12
Example of machine-language
  • Heres 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

13
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!

14
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 machines design
  • Lets see our example-program, rewritten using
    actual assembly language for Intels Pentium

15
Simplified Block Diagram
Central Processing Unit
Main Memory
system bus
I/O device
I/O device
I/O device
I/O device
16
The visible x86 registers
RAX
RSP
R8
R12
RBX
RBP
R9
R13
RCX
RSI
R10
R14
RDX
RDI
R11
R15
CS
DS
ES
FS
GS
SS
RIP
RFLAGS
Intel Core-2 Quad processor
17
The Fetch-Execute Cycle
main memory
central processor
Temporary Storage (STACK)
RSP
Program Variables (DATA)
EAX
EAX
EAX
RAX
Program Instructions (TEXT)
RIP
the system bus
18
Define symbolic constants
  • .equ device_id, 1
  • .equ sys_write, 1
  • .equ sys_exit, 60

19
our programs data section
  • .section .data
  • x .quad 4
  • y .quad 5
  • fmt .asciz d d d \n

20
Our programs bss section
  • .section .bss
  • z .quad 0
  • n .quad 0
  • buf .space 80

21
our programs text section
  • .section .text
  • _start
  • comment assign z x y
  • movl x, rax
  • addl y, rax
  • movl rax, z

22
text section (continued)
  • comment prepare programs output
  • push z arg 5
  • push y arg 4
  • push x arg 3
  • push fmt arg 2
  • push buf arg 1
  • call sprintf function-call
  • addl 40, rsp discard the args
  • movl rax, n save return-value

23
text section (continued)
  • comment request kernel assistance
  • movl sys_write, rax
  • movl device_id, rdi
  • movl buf, rsi
  • movl n, rdx
  • syscall

24
text section (concluded)
  • comment request kernel assistance
  • movl sys_exit, rax
  • movl 0, rdi
  • syscall
  • comment make label visible to linker
  • .global _start
  • .end

25
program translation steps
demo.s
demo.o
program source module
demo
program object module
assembly
the executable program
linking
object module library
object module library
other object modules
26
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)

27
How a program is loaded
0xFFFFFFFFFFFFFFFF
Kernels code and data
stack
runtime libraries
.bss
uninitialized variables
.data
initialized variables
.text
program instructions
0x0000000000000000
Main memory
28
What must programmer know?
  • Needs to use CPU register-names (rax)
  • Needs to know space requirements (quad)
  • Needs to know how stack works (push)
  • Needs to make symbol global (for linker)
  • Needs to understand how to quit (exit)
  • And of course how to use system tools
  • (e.g., text-editor, assembler, and linker)

29
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 are readily portable
  • Low-level programs tied to specific CPU

30
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 application using
  • ./demo1

31
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

32
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

33
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

34
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
Write a Comment
User Comments (0)
About PowerShow.com