77
mith College Computer Science Dominique Thiébaut [email protected] CSC231-Assembly Week #4 Fall 2019

CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

mith College

Computer Science

Dominique Thiébaut [email protected]

CSC231-AssemblyWeek #4 Fall 2019

Page 2: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Logistics

• Midterm Exam

• Oct 21st.

• In class.

• On paper.

• Closed books, closed notes, closed computers.

Page 3: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Outline• More Hexadecimal Notation

• Creating a listing with opcodes

• Seg Faults

• Hexdump

• mov & add

• Registers

Page 4: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Remem

ber…

Nerd Alert!Nerd Alert!Remem

ber…

Page 5: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

11 section .data 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello 15 16 section .text 17 global _start 18 _start: 19 20 ;;; print message 21 00000000 B804000000 mov eax, 4 ; write 22 00000005 BB01000000 mov ebx, 1 ; stdout 23 0000000A B9[00000000] mov ecx, Hello ; 24 0000000F BA0E000000 mov edx, HelloLen ; 25 00000014 CD80 int 0x80 26 27 ;;; exit 28 00000016 BB00000000 mov ebx, 0 29 0000001B B801000000 mov eax, 1 30 00000020 CD80 int 0x80

Page 6: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Two Tips forAssembly Language Programmers

• Seeing invisible characters

• Segmentation faults

Page 7: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Seeing Invisible Characters

Page 8: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Seeing Invisible Characters

Page 9: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Seeing Invisible Characters

Page 10: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Page 11: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Two Tips forAssembly Language Programmers

• Seeing invisible characters

• Segmentation faults

Page 12: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

segFault

Page 13: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

segFault

Page 14: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Segments

RAM

Code

DataAd

dres

ses

Page 15: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Hexdump

Page 16: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Visualizing Bytes in RAM(Almost!)

emacs hello.asm

nasm -f elf hello.asmld -melf_i386 hello. -o hello./hello

nasm -f elf hello.asmld -melf_i386 hello. -o hellohexdump -v -C hello

Page 17: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Hexdump

Page 18: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

11 section .data 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello 15 16 section .text 17 global _start 18 _start: 19 20 ;;; print message 21 00000000 B804000000 mov eax, 4 ; write 22 00000005 BB01000000 mov ebx, 1 ; stdout 23 0000000A B9[00000000] mov ecx, Hello ; 24 0000000F BA0E000000 mov edx, HelloLen ; 25 00000014 CD80 int 0x80 26 27 ;;; exit 28 00000016 BB00000000 mov ebx, 0 29 0000001B B801000000 mov eax, 1 30 00000020 CD80 int 0x80

Page 19: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Our Goal forThis Weekint x, y, sum;

x = 3;y = 5;sum = x + y;

Page 20: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Sub-Outline

• mov & add instructions

• Registers

• Memory storage directives

Page 21: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

You already knowsome of this material…

• https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Page 22: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

The mov instruction

mov dest, source

Page 23: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

The mov instruction

mov dest, source

copy

Page 24: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

The mov instruction

mov dest, source

11 section .data 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello 15 16 section .text 17 global _start 18 _start: 19 20 ;;; print message 21 00000000 B804000000 mov eax, 4 ; write 22 00000005 BB01000000 mov ebx, 1 ; stdout 23 0000000A B9[00000000] mov ecx, Hello ; 24 0000000F BA0E000000 mov edx, HelloLen ; 25 00000014 CD80 int 0x80 26 27 ;;; exit 28 00000016 BB00000000 mov ebx, 0 29 0000001B B801000000 mov eax, 1 30 00000020 CD80 int 0x80

copy

Page 25: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Quick Look Back at Simulator

Page 26: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Pentium Registers

eax

ebx

ecx

edx

Pentium Processor

Page 27: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Operands

• mov reg, reg

• mov reg, mem

• mov mem, reg

• mov reg, imm

• mov mem, imm

Page 28: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

section .data a dd 1234

section .text mov eax, 34 mov ebx, 12345

mov edx, eax mov ecx, ebx

eax

ebx

ecx

edx

Page 29: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

section .data a dd 1234

section .text mov eax, dword[a] mov ebx, eax

mov eax, 1234 mov dword[a], eax

eax

ebx

ecx

edx

Page 30: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

section .data hello db "Hi!" helloL equ $-hello a dd 1234

section .text mov eax, 123456789 mov dword[a], eax

mov dword[a], 0 mov ecx, dword[a]

eax

ebx

ecx

edx

Page 31: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

The add instruction

add dest, source

Page 32: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

The add instruction

add dest, source

dest = dest + source

Page 33: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

section .data a dd 1234

section .text mov eax, 3 mov ebx, 5

add eax, ebx

eax

ebx

ecx

edx

Page 34: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

section .data a dd 1234

section .text mov eax, dword[a] add eax, 1

mov dword[a], eax

eax

ebx

ecx

edx

Page 35: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

section .data a dd 1234

section .text add dword[a], 1

eax

ebx

ecx

edx

Page 36: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Exerciseint x, y, sum;

x = 3;y = 5;sum = x + y;

Translate this into Assembly

Page 37: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

We stopped here last time…We stopped here last time…

Page 38: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

A Quick Introductionto the next Homework

Assignment…

getcopy hw4_1_skel.asm

cp hw4_1_skel.asm hw4_1.asm

getcopy 231Lib.asm

nasm -f elf hw4_1.asm

nasm -f elf 231Lib.asm (do only once!)

ld -melf_i386 hw4_1.o 231Lib.o -o hw4_1

Page 39: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

eax

ebx

ecx

edx

ax

bx

cx

dx

Pentium Registers

Page 40: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

eax

ebx

ecx

edx

ax

bx

cx

dx

ah al

bh bl

ch cl

dh dl

Pentium Registers

Page 41: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Think of ah and al as boxes inside

a bigger one called ax, and ax as

half of a bigger box still,

called eax.

Page 42: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Pentium Registers

11111111 11111111 11111111 1111111132 bits 4,294,967,295

11111111 1111111116 bits 65,535

111111118 bits 255

11114 bits 15

largest unsigned

Page 43: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Pentium Registers

F F F F F F F F32 bits 4,294,967,295

F F F F 16 bits 65,535

F F 8 bits 255

F 4 bits 15

largest unsigned0000 00001 10010 20011 30100 40101 50110 60111 71000 81001 91010 A1011 B1100 C1101 D1110 E1111 F

Page 44: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Declaring Variables

Page 45: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

db, dw, dd

• db: define byte storage

• dw: define word storage

• dd: define double-word storage

Page 46: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Examples: db

msg db "Hello", 10

a db 0b db 'H' ; also 72 or 0x48c db 255d db 0x80

Page 47: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Examples: dw

x dw 0y dw 1z dw 255t dw 0x1234

Page 48: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Examples: dd

alpha dd 0beta dd 255gamma dd 0x12345678

Page 49: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Summary ofimportant concepts just seen

• Numbers

• Op Codes

• Machine Language

• Hexadecimal

• Executable Files

Page 50: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

More Examples ofThe mov Instruction

mov dest, source

Page 51: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

section .datalf db 10c db 0a dw 0x1234b dw 0x dd 0y dd 0x12345678

section .text; put char lf in al

Exercise 1

eax

ebx

ecx

edx

ax

bx

cx

dx

ah al

bh bl

ch cl

dh dl

Page 52: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

section .datalf db 10c db 0a dw 0x1234b dw 0x dd 0y dd 0x12345678

section .text; copy al to Variable c

Exercise 2

eax

ebx

ecx

edx

ax

bx

cx

dx

ah al

bh bl

ch cl

dh dl

Page 53: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

section .datalf db 10c db 0a dw 0x1234b dw 0x dd 0y dd 0x12345678

section .text; put a in bx

; put bx in b

; put bx in ax

; put 0 in cx

Exercise 3

eax

ebx

ecx

edx

ax

bx

cx

dx

ah al

bh bl

ch cl

dh dl

Page 54: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

section .datalf db 10c db 0a dw 0x1234b dw 0x dd 0y dd 0x12345678

section .text; put x in eax

; put y in ecx

; put ecx in edx

; put ex into y

Exercise 4

eax

ebx

ecx

edx

ax

bx

cx

dx

ah al

bh bl

ch cl

dh dl

Page 55: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

section .datalf db 10c db 0a dw 0x1234b dw 0x dd 0y dd 0x12345678

section .text; put 0 in ah

; put 3 in cx

; put 5 in edx

; put 0x12345678 into eax

Exercise 5

eax

ebx

ecx

edx

ax

bx

cx

dx

ah al

bh bl

ch cl

dh dl

Page 56: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

We understand mov!

We understand add!

Page 57: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

More add Addressing Modes

add dest, source

Page 58: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

section .datalf db 10ch db 0a dw 0x1234b dw 0x dd 0y dd 0x12345678

section .text; add 3 to Variable c

; add 100 to b

; add -1 to edx

; add x to y

Example

eax

ebx

ecx

edx

ax

bx

cx

dx

ah al

bh bl

ch cl

dh dl

Page 59: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Reminder: Our Goal was…

int x, y, sum;

x = 3;y = 5;sum = x + y;

Page 60: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Reminder: Our Goal was…

int x, y, sum;

x = 3;y = 5;sum = x + y;

Translate this into Assembly,

once more

Page 61: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Reminder: Our Goal was…

short x, y, sum;

x = 3;y = 5;sum = x + y;

Translate this new program into Assembly

Typical

Midterm

Question

Page 62: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Exercise 1section .dataa db 10b db 0c dw 0x1234d dw 0e dd 0f dd 0x12345678

section .text

Swap a and b. Then c and d. Then e and f.

Do on

your own!

Share result

on Piazza.

Correct if faulty

Page 63: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Exercise 2section .dataa db 10b db 0c dw 0x1234d dw 0e dd 0xcdeff dd 0x12345678

section .text

Set the least significant byte of e and f to 00.

Do on

your own!

Share result

on Piazza.

Correct if faulty

Page 64: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Exercise 3section .dataa db b db c dw d dw e dd f db

section .text

reconstruct the declarations of a, b, c, d, e

and f.

99

88

77

66

55

44

33

22

1f

1a

11

00 a

typical

midterm

question!

hex

Do on

your own!

Share result

on Piazza.

Correct if faulty

Page 65: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Exercise 4section .dataa db b db c dw d dw e dd f db

section .text

reconstruct the declarations of a, b, c, d, e

and f.

99

88

00

00

55

44

33

22

31

26

11

00 a

typical

midterm

question!

dec

Do on

your own!

Share result

on Piazza.

Correct if faulty

Page 66: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Same with Bytes

byte x, y, sum;

x = 3;y = 5;sum = x + y;

Translate this into Assembly

Page 67: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

With an Array!

int A[3];

A[0] = 3;A[1] = 5;A[2] = A[0]+A[1]

Translate this into Assembly

Page 68: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Exercisesection .dataa db b db c dw d dw e dd f db

section .text

reconstruct the declarations of a, b, c, d, e

and f.

99

88

77

66

55

44

33

22

31

26

11

00 a

typical

midterm

question!

decimal

Page 69: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Follow a stepby step execution

of the program

Page 70: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

8 9 section .data 10 00000000 03000000 a dd 3 11 00000004 05000000 b dd 5 12 00000008 00000000 sum dd 0 13 14 ;;; 15 ;;; code area 16 ;;; 17 18 section .text 19 global _start

20 00000000 A1[00000000] _start: mov eax, dword[a] ;eax <-- a 21 00000005 0305[04000000] add eax, dword[b] ;eax <-- eax+b = a+b 22 0000000B 83E801 sub eax, 1 ;eax <-- eax-1 = a+b-1 23 0000000E A3[08000000] mov dword[sum], eax ;sum <-- eax = a+b-1

24 ;;; exit() 25 00000013 B801000000 mov eax,1 26 00000018 BB00000000 mov ebx,0 27 0000001D CD80 int 0x80 ; final system call

Page 71: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

a dd 3b dd 5sum dd 0

100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax

eax

ebx

ecx

edx

eip

Page 72: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

a dd 3b dd 5sum dd 0

100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax

eax

ebx

ecx

edx

eip

a dd 3b dd 5sum dd 0

100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax

eax

ebx

ecx

edx

eip

Tick!

Page 73: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

a dd 3b dd 5sum dd 0

100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax

eax

ebx

ecx

edx

eip

a dd 3b dd 5sum dd 0

100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax

eax

ebx

ecx

edx

eip

a dd 3b dd 5sum dd 0

100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax

eax

ebx

ecx

edx

eip

Tick!

Page 74: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

a dd 3b dd 5sum dd 0

100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax

eax

ebx

ecx

edx

eip

a dd 3b dd 5sum dd 0

100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax

eax

ebx

ecx

edx

eip

a dd 3b dd 5sum dd 0

100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax

eax

ebx

ecx

edx

eip

a dd 3b dd 5sum dd 0

100 mov eax, dword[a]105 add eax, dword[b]10A sub eax, 110E mov dword[sum], eax

eax

ebx

ecx

edx

eip

Tick!

Page 75: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Frequency: 3.2 GHz

cycle: 1/3.2 GHz =0.3125 ns

sec ms us ns

Page 76: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Arduino

;hello.asm; turns on an LED which is connected to PB5 (digital out 13)

.include "./m328Pdef.inc"

ldi r16,0b00100000out DDRB,r16out PortB,r16

Start:rjmp Start

Clock speed: 16 MHz

~1/200 speed of Pentium

http://www.science.smith.edu/dftwiki/index.php/Comparing_Different_Computers_with_N_Queens_Program

Page 77: CSC231-Assembly · 12 00000000 48656C6C6F20746865- Hello db "Hello there!", 10, 10 13 00000009 7265210A0A 14 HelloLen equ $-Hello ... Two Tips for Assembly Language Programmers

D. Thiebaut, Computer Science, Smith College

Raspberry Pi

/* -- first.s *//* This is a comment */.global main /* 'main' is our entry point and must be global */ main: /* This is main */ mov r0, #2 /* Put a 2 inside the register r0 */ bx lr /* Return from main */

Clock speed: 1.4 GHz

~1/3 speed of Pentium

http://www.science.smith.edu/dftwiki/index.php/Comparing_Different_Computers_with_N_Queens_Program