Modul PBM 4 (Assembly Basic)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

MODUL PERKULIAHAN

PERANCANGAN
BERBASIS
MIKROPROSESOR

Assembly Basic

Abstrak Sub-CPMK (lihat di RPS)

Mempelajari konsep mampu memahami Prinsip Kerja Intel 8051,


pemrograman ROM pada Komunikasi Serial 8051, Akses LCD, Serial BUS,
mikroprosesor inter 8051 Mengisi Program ke dalam ROM internal,
Sistem minimum 8051

Why learning Assembler?


2021 Pengantar Ilmu Bahan
1 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Assembler or other languages, that is the question. Why should I learn another
language, if I already learned other programming languages? The best argument: while you
live in France you are able to get through by speaking English, but you will never feel at
home then, and life remains complicated. You can get through with this, but it is rather
inappropriate. If things need a hurry, you should use the country's language.
Many people that are deeper into programming AVRs and use higher-level
languages in their daily work recommend that beginners start with learning assembly
language. The reason is that sometimes, namely in the following cases: ● if bugs have to
be analyzed, ● if the program executes different than designed and expected, ● if the
higher-level language doesn't support the use of certain hardware features, ● if time-critical
in line routines require assembly language portions,
Short and easy
Assembler instructions translate one by one to executed machine instructions. The
processor needs only to execute what you want it to do and what is necessary to perform
the task. No extra loops and unnecessary features blow up the generated code. If your
program storage is short and limited and you have to optimize your program to fit into
memory, assembler is choice 1. Shorter programs are easier to debug, every step makes
sense.
Fast and quick
Because only necessary code steps are executed, assembly programs are as fast as
possible. The duration of every step is known. Time critical applications, like time
measurements without a hardware timer, that should perform excellent, must be written in
assembler. If you have more time and don't mind if your chip remains 99% in a wait state
type of operation, you can choose any language you want.

2021 Pengantar Ilmu Bahan


2 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
How High-Level Language Compiled

Other programming
language

Python (pip), JAVA(JVM),


Nodejs(npm)

How Low-Level Language Compiled

2021 Pengantar Ilmu Bahan


3 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Executing Instructions

The CPU reads hex 0C01, R0 and R1 is added and the result is written to register
R0. This is executed like demonstrated in the picture.
First the instruction word (16 bit) is read from the flash and translated to executable
steps (1). The next step connects the registers to the ALU inputs, and adds their content
(2). Next, the result is written to the register (3).
If the CPU reads hex 9C23 from the flash, the registers R3 and R2 are muliplied and
the result is written to R1 (upper 8 bits) and R0 (lower 8 bits). If the ALU is not equipped
with hardware for multiplication (e. g. in an ATtiny13), the 9C23 does nothing at all. It doesn't
even open an error window (the tiny13 doesn't have that hardware)!
In principle the CPU can execute 65,536 (16-bit) different instructions. But because
not only 170 should be written to a specific register, but values between 0 and 255 to any
register between R16 and R31, this load instruction requires 256*16 = 4,096 of the 65,536
theoretically possible instructions. The direct load instruction for the constant c (c7..c0) and
registers r (r3..r0, r4 is always 1 and not encoded) is coded like this:

Addition and subtraction require 32*32 = 1,024 combinations and the target registers
R0..R31 (t4..t0) and source registers R0..R31 (s4..s0) are coded like this:

2021 Pengantar Ilmu Bahan


4 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Instructions in Assembler
There is no need to learn 16-bit numbers and the crazy placement of bits within
those, because in assembler you'll use human-readable abbreviations for that, so-called
mnemonics, an aid to memory. The assembler representation for hex 9588 is simply the
abbreviation "SLEEP". In contrast to 9588, SLEEP is easy to remember. Even for someone
like me that has difficulties in remembering its own phone number.
Adding simply is "ADD". For naming the two registers, that are to be added, they are
written as parameters. (No, not in brackets. C programmers, forget those brackets. You
don't need those in assembler.) Simply type "ADD R0,R1". The line translates to a single
16 bit word, 0C01. The translation is done by the assembler.
The CPU only understands 0C01. The assembler translates the line to this 16 bit
word, which is written to the flash storage, read from the CPU from there and executed.
Each instruction that the CPU understands has such a mnemonic. And vice versa: each
mnemonic has exactly one corresponding CPU instruction with a certain course of actions,

Assembler Code

2021 Pengantar Ilmu Bahan


5 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Assembler Code

LD A, 01H

LD = LOAD

A = Register A inside the CPU

01 = number loaded into register A

H = Hexadecimal part of 01

2021 Pengantar Ilmu Bahan


6 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Assembler Code

2021 Pengantar Ilmu Bahan


7 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Atmel Studio 7

Atmel Studio

Atmel Studio 7 Welcome Screen

2021 Pengantar Ilmu Bahan


8 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Start new project by click menu File >> New >> Project
Or with shortcut button CTRL+SHIFT+N

Fill the Project name


PBM04_NAMA_NIM

Fill the Project name

PBM04_NAMA_NIM

You might choose installed project in the left side.


ATMega328 can be programmed with C/C++ in which common language (High-Level
Language in any case can be programmed by using Arduino Software).
In this case please follow the instructions by click Assembler

2021 Pengantar Ilmu Bahan


9 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
You can download some documentations here. (please download the datasheet once)

4
Keterangan:
1. Menu Bar
2. Code Editor
3. Solution Explorer
4. Log Bar

2021 Pengantar Ilmu Bahan


10 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Type the started code then copy
m328p.inc inside

C:\Users\pc_name\Documents\
Atmel
Studio\7.0\PBM04_NAMA_NIM\
PBM04_NAMA_NIM\

2021 Pengantar Ilmu Bahan


11 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Click button start debugging

or click Debug continue (F5)

2021 Pengantar Ilmu Bahan


12 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Select -> Simulator

to shows the process inside


the CPU give an attention,
follows:

Click step into (F11)

2021 Pengantar Ilmu Bahan


13 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Code Explanations

Everything after a semicolon ‘;’


is ignored by the assembler and
hence these first two lines are
simply "comments" explaining
what the program does.

Pin Mapping

2021 Pengantar Ilmu Bahan


14 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Code Explanations

This line tells the assembler to include the m328p.inc file which you downloaded. You may
want to put this in a directory of similar include files and then change the above line to
point to it there.

ldi hregister, numberloads a number (0-255) into a upper half register (16-31)

2021 Pengantar Ilmu Bahan


15 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
ldi stands for "load immediate" and tells the assembler to take a working register,
r16 in this case, and load a binary number into it, 0b00100000 in this case. The 0b in front
says that our number is in binary. If we wanted we could have chosen another base, such
as hexidecimal. In that case our number would have been 0x20 which is hexidecimal for
0b00100000. Or we could have used 32 which is base 10 decimal for the same number.

Using binary is simplest though because of the way Ports and Registers work. We
will discuss the ports and registers of the atmega328p in more detail in future tutorials but
for now I'll just state that we are using r16 as our "working register" meaning that we are
just going to use it as a variable that we store numbers in. A "register" is a set of 8 bits.
Meaning 8 spots that can either be 0 or 1 (`off' or `on'). When we load the binary number
0b00100000 into the register using the above line we have simply stored that number in the
register r16.

2021 Pengantar Ilmu Bahan


16 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
out i/o register, register copies a number from a working register to an I/O register
This line tells the compiler to copy the contents of the register r16 into the DDRB
register. DDRB stands for "Data Direction Register B" and it sets up the "pins" on PortB. On
the pinout map for the 328p you can see that there are 8 pins labeled PB0, PB1, ... , PB7.
These pins represent the "bits" of "PortB" and when we load the binary number 00100000
into the DDRB register we are saying that we want PB0, PB1, PB2, PB3, PB4, PB6, and
PB7 set as INPUT pins since they have 0's in them, and PB5 is set as an OUTPUT pin
since we put a 1 in that spot.

The in the left side line copies the


same binary number from our
storage register r16 to PortB. This
sets all of the pins to 0 volts except
pin PB5 to HIGH which is 5 volts.

2021 Pengantar Ilmu Bahan


17 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
rjmp label jumps to the line of the program labeled by "label" (which cannot be further than 204
instructions away -- i.e. relative jump) -> NEXT LECTURE
The first line above is a "label" which labels a spot in the code. In this case
labelling that spot as "Start". The second line says "relative jump to the label Start." The
net result is that the computer is placed into an infinite loop that just keeps cycling back to
Start. We need this because we cannot have the program just end, or fall off a cliff, the
program has to just keep running in order for the light to stay lit.

Exercise
Please create a simulation using PROTEUS 7/8 of the following code above.

Find by yourself some reference to do it.

Please submit the result as a video (using screen recorder software that I’ve been attached on
GoogleDrive). The video explains the whole process of what you have done.

Post the video on the Forum or Email (in case POST system while under maintenance)
BETTER BOTH OF THEM.

Email subject and file name MUST BE FORMATTED PBM04_NAMA_NIM.

DEADLINE APRIL,03,2020

Thanks for your advanced

2021 Pengantar Ilmu Bahan


18 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/
Daftar Pustaka

• Charles M. Gilmore, “Microprocessor : Principles and Application”,


Glencoe/Mc.Graw-Hill, 2nd International Editions, 1998.
• John Crisp,(2004),Introduction Microprocessors and Microcontrollers (2nd
Edition),an imprint of Elsevier, ISBN: 0-7506-5989-0.
• Lance A. Leventhal, “Introduction to Microprocessors : Software, Programming”,
Prentice Hall, 1998
• Barry B. Brey, “Microprocessors and Peripherals : Hardware, Software, Interfacing,
and Applications”, Merrill Publishing Company 1998.
• Barry B. Brey, “The Intel Microprocessors 80386/80486, Pentium and Pentium
Processor”, Prentice Hall / Mac Millan 4/e, 1997
• David Calcutt, Fred Cowan and Hassan Parchizadeh,(2004),8051 Microcontrollers
–An Applications Based Introduction, an imprint of Elsevier, ISBN: 0-7506-5759-6
• Jack Purdum,(2012),Beginning C for Arduino, Published by by Springer Science,
ISBN:978-1-4302-4777-7
• Michael Margolis,(2011),Arduino Cookbook, Published by O’Reilly Media,
Inc.,ISBN: 978-0-596-80247-9

2021 Pengantar Ilmu Bahan


19 Zendi Iklima, M.Sc
Biro Bahan Ajar E-learning dan MKCU
http://pbael.mercubuana.ac.id/

You might also like