組合語言 (Assembly Language)

33
組合語言 (Assembly Language) 組合語言 (Assembly Language) Peng-Sheng Chen Fall, 2021

Transcript of 組合語言 (Assembly Language)

組合語言(Assembly Language)

組合語言(Assembly Language)

Peng-Sheng Chen

Fall, 2021

2

IntroductionIntroduction

課程名稱:組合語言

科目代碼:4102050-01

時間:Monday (C) , Wednesday (C)

10:15~11:30

教室:遠距教學 (同步/非同步/面授)

課程網頁:請參閱 Ecourse2

(請確認Ecourse2上的個人email address是正確的,並時常察看訊息)

Pre-requirementPre-requirement

計算機概論

Linux/UNIX-like OS之基本操作

C programming language

3

4

高階語言

int main(void)

{

printf(“Hello World\n”);

return 0;

}

5

機器語言

6

MOV r1,#100

LDR r2,[r3]

LOOP

LDMIA r9! , {r0-r7}

STMIA r10!, {r0-r7}

CMP r9 , r11

BNE LOOP

...

處理器指令(低階程式語言)

7

MOV r1,#100

LDR r2,[r3]

LOOP

LDMIA r9! , {r0-r7}

STMIA r10!, {r0-r7}

CMP r9 , r11

BNE LOOP

...

int main(void)

{

printf(“Hello World\n”);

return 0;}

高階語言

Compiler

Assembler

組合語言組合語言

在程式裡使用:C、C++、Java等多種語言,組合成為應用程式

組合語言是一種接近機器碼的低階程式語言

每一種處理器,有它自己的指令

透過撰寫處理器的指令(即為撰寫組合語言)而形成程式

與底層硬體有非常密切的關係

有些文件翻譯成: 彙編語言8

9

TargetTarget

瞭解電腦的低階機器程式語言

學習組合語言,並瞭解組合語言在電腦程式、作業系統、與硬體設備間所扮演的角色

學習ARM assembly language

10

TopicsTopics

Computer system and system software

ARM architecture

ARM/Thumb programming model and ARM

instruction set

Data processing instructions

Data transfer instructions

Control-flow instructions

Software interrupt (SWI)

Conditional execution

SIMD instructions (x86)

RISC-V

11

Applications

High-level Language

Assembly Language

Machine Language

Hardware / OS

system calls

System

independent

System

dependent0101111011011

0111100010110

add r1, r2, r3

a = b + c

12

MOV r1,#100

LDR r2,[r3]

LOOP

LDMIA r9! , {r0-r7}

STMIA r10!, {r0-r7}

CMP r9 , r11

BNE LOOP

...

int main(void)

{

printf(“Hello World\n”);

return 0;}

高階語言

Compiler

Assembler

13

System SoftwareSystem Software

Compiler

Assembler

demo.c

demo.s

demo.o

Linker

Executable file

Compiler

Assembler

demo.c

demo.s

demo.o

Linker

Executable file

func1.s

func1.o

14

ExampleExample

int main(void)

{

int x = 100;

double u;

u = x + 100;

return 0;

}

.file "test.c"

.def

___main; .scl

2; .type 32; .endef

.text

.globl _main

.def _main; .scl

2; .type 32; .endef

_main:

pushl %ebp

movl %esp, %ebp

subl $24, %esp

andl $-16, %esp

movl $0, %eax

addl $15, %eax

addl $15, %eax

shrl $4, %eax

sall $4, %eax

movl %eax, -20(%ebp)

movl -20(%ebp), %eax

call __alloca

call ___main

movl $100, -4(%ebp)

movl -4(%ebp), %eax

C code

Assembly code (x86)

15

Why should we learn ARM assembly language ?

16

ARM Powered ProductsARM Powered Products

Reference from www.arm.com

ARM Powered ProductsARM Powered Products

Automotive

Healthcare

Internet of Things

Smart homes

Mobile computing

Wearables

17

18

ARM Powered Products

Lineable, A smart solution to

help keep kids safe (ARM Cortex M10)

Smart watch

(ARM Cortex M3, M0)

19

Neo, Smart pen (ARM926EJ-S)

LifeBEAM, Smart headwear

(ARM Cortex M3)

Dobby (ZeroTech), Snapdragon 801

ARMv7 ISA

20

Nintendo Switch (4-core, ARM Cortex-A57, ARM Cortex-A53)

Playstation vita

(4-core, ARM Cortex A9)

Audi AG (Tegra 3,

4-core ARM Cortex A9)

OthersOthers

RISC-V

MIPS Open

x86

21

22

Why should we learn x86 assembly language ?

(x86, IA32)

23

x86 Instruction Setx86 Instruction Set

x86普遍使用於個人電腦

24

Why Use Assembly LanguageWhy Use Assembly Language

Performance

For high level languages, the abstraction

means that the programmer never has to worry

about the details of the computer.

編譯器(Compiler)有時候仍然無法產生高效能的組合語言

Ex: SIMD instructions

Assembly language is a good way to

understand exactly what the computer is doing

because you control exactly what happens at

each step.

25

Why Use Assembly LanguageWhy Use Assembly Language

Code size

Ex: embedded systems

Application

Driver

Accessibility to hardware

26

Reference (1)Reference (1)

Lecture notes by the

instructor

Title: ARM Assembly

Language: Fundamentals

and Techniques, Second

Edition

Author: William Hohl,

Christopher Hinds

Publisher: CRC press

27

Reference (2)Reference (2)

Lecture notes by the instructor

Title: ARM Assembly

Language – an Introduction

Author: J. R. Gibson

ISBN: 978-1-4477-1715-7

Publisher: lulu.com

28

Reference (3)Reference (3)

Title: ARM system-on-chip

architecture

Author: Steve Furber

ISBN: 0201675196

Publisher: Addison-Wesley

Local sales representative:

新月圖書

29

GradingGrading

Homework: 55% (非常重要)

Quiz: 10%

Midterm exam: 15%

Final exam: 15%

Class attendance, active participation: 5%

•作業抄襲:零分•考試作弊:零分

Any Question?

BackupBackup

31

32

ASUS ZenFone Zoom

Lenovo K900 / K8

Intel Edison board

33

int main(void)

{

printf(“Hello World\n”);

return 0;

}

機器語言高階語言