Você está na página 1de 9

Experiment 4

Assembly Language Programming Fun-


damentals

Lab Objective

In this lab we will learn some basic data processing instructions and observe how they are used
in different scenarios.

Introduction to Assembly Language

Every computer no matter how simple or complex has a CPU and memory and these two
things are responsible for running your computer program. Basically, a computer program is
a collection of numbers stored in memory in the form of ones and zeros. CPU reads these
numbers one at a time, decodes them and perform the required action. We term these numbers
as machine language.

Although machine language instructions make perfect sense to computer but humans cannot
comprehend them. A long time ago, someone came up with the idea that computer programs
could be written using words instead of numbers and a new language of mnemonics was de-
veloped and named as assembly language. An assembly language is a low-level programming
language and there is a very strong (generally one-to-one) correspondence between the language
and the architectures machine code instructions.

We know that computer cannot interpret assembly language instructions. A program should be
developed that performs the task of translating the assembly language instructions to machine
language. A computer program that translates the source code written with mnemonics into
the executable machine language instructions is called an assembler. The input of an assembler
is a source code and its output is an executable object code.

Advantages of Assembly Language

With the development of modern optimizing compilers for high level languages the use of assem-
bly language has been considerably reduced. However, assembly language has various benefits
due to which it is still preferred in some areas.

31
32 CHAPTER 4. ASSEMBLY LANGUAGE PROGRAMMING FUNDAMENTALS

Speed

Assembly language programs are generally the fastest programs among the programming lan-
guages. By careful coding in assembly language you can speed up many programs by a factor
of five or ten over their HLL counterparts.

Space

Assembly language programs are often less than one-half the size of comparable HLL programs.
Saving space saves money. If an application requires 2 MB of RAM, the user will have to install
an extra megabyte if there is only one available in the machine. Even on big machines with
larger memory, writing gigantic applications isnt excusable. Most users want to run multiple
programs from memory at one time. The bigger a program is, the fewer applications will be
able to coexist in memory with it. Virtual memory isnt a particularly attractive solution either.
With virtual memory, the bigger an application is, the slower the system will run as a result of
that programs size.

Capability

HLLs are an abstraction of a typical machine architecture. They are designed to be independent
of the particular machine architecture. As a result, they rarely take into account any special fea-
tures of the machine, features which are available to assembly language programmers. Assembly
language provides a faclilty to write a code that must interact directly with the hardware, for
example in device drivers and interrupt handlers.

Knowledge

Although few programmers today regularly work with assembly language as a tool, the under-
lying concepts remain very important. Fundamental topics such as binary arithmetic, memory
allocation, stack processing, character set encoding, interrupt processing, and compiler design
would be hard to study in detail without a grasp of how a computer operates at the hardware
level. Since a computers behavior is fundamentally defined by its instruction set, the logical
way to learn such concepts is to study an assembly language.

Disadvantages of Assembly Language

Here we discuss some drawbacks of assembly language programming which have led to its
reduced usage as a programming language.
33

Hard to Learn, Read and Understand

Although weve made a few improvements by eliminating hex code, the command names are not
always clear. Moreover, to program in assembly language you must have a detailed knowledge
of microcomputer you are using. You must know what register and instructions this microcom-
puter has, affect of various instructions on different registers and a lot more. These details are
not relevant to application we want to implement. This makes assembly language hard to learn
and understand.

Hard to Maintain and Debug

Finding mistakes in assembly code is difficult. Correcting them or adding new features can also
be a challenge.

Machine Dependent

Assembly language programs are not portable. We havent really moved that far away from the
machine language rather than just putting some psuedo-english labels on it. We still need to
rewrite every piece of code for every machine. For example, program written for ARM platform
will not run on Intel processor.

Assembly Language Syntax

ARM assembler commonly uses following instruction format:

label
opcode operand1, operand2, ; Comment

Normally, the first operand is the destination of the operation. The number of operands in an
instruction depends on the type of instruction, and the syntax format of the operand can also
be different.

Label

Label is an optional first field of an assembly statement. Labels are alphanumeric names used
to define the starting location of a block of statements. Labels can be subsequently used in
our program as an operand of some other instruction. When creating the executable file the
assembler will replace the label with the assigned value. Labels must be unique in the executable
file because an identical label encountered by the Assembler will generate an error.
34 CHAPTER 4. ASSEMBLY LANGUAGE PROGRAMMING FUNDAMENTALS

ARM assembler has reserved first character of a line for the label field and it should be left
blank for the instructions with no labels. In some compilers, labels can be optionally ended
with colon(:) but it is not accepted by the ARM assembler as an end of the label field.

Defining appropriate labels makes your program look more legible. Program location can be
easily found and remembered using labels. It is easier to use certain functionality of your
program in an entirely different code .i.e., your code can be used as a library program. You
do not have to figure out memory addresses because it is a tedious task especially when the
instruction size in your microcontroller is not fixed.

Opcode (Mnemonics)

Opcode is the second field in assembly language instruction. Assembly language consists of
mnemonics, each corresponding to a machine instruction. Using a mnemonic you can decide
what operation you want to perform on the operands. Assembler must translate each mnemonic
opcode into their binary equivalent.

Operands

Next to the opcode is the operand field which might contain different number of operands.
Some of the instructions in Cortex-M3 will have no operand while other might have as many as
four operands. The number of operands in an instruction depends on the type of instruction,
and the syntax format of the operand can also be different. Normally, the first operand is the
destination of the operation.

Comments

Comments are messages intended only for human consumption. They have no effect on the
translation process and indeed are not acted on by the ARM Assembler. The comment field
of an assembly language instruction is also optional. A semicolon signies that the rest of the
line is a comment and is to be ignored by the assembler. If the semicolon is the rst non-blank
character on the line, the entire line is ignored. If the semicolon follows the operands of an
instruction, then only the comment is ignored by the assembler. The purpose of comments is
to make the program more comprehensible to the human reader. If you want to modify the
program in response to a product update and you havent used that code before then comments
go a long way to helping comprehension.
35

Shift and Rotate instructions

Shift and Rotate instructions are used to change the position of bit values in a register. Different
variants of these instructions are discussed in this manual.

Logical Shift Right (LSR)

This instruction is similar to the unsigned divide by 2n where n number of shifts. Execute the
following instructions in your compiler and observe the results.
MOV R0 , #0x06
; Move 0 x06 t o l o w e r byte o f R0

MOV R3 , #0x200
; Move t h e 12 b i t hex number 200 t o t h e low p o r t i o n o f R5

MOV R5 , #0x9B1D
; Move t h e 16 b i t hex number 9B1D t o t h e low p o r t i o n o f R5

STOP
LSR R4 , R3 , #6 ; Logical shift right by 6 bits
LSR R2 , R3 , R0 ; Logical shift right by t h e v a l u e i n l o w e r byte o f R0
LSR R6 , R5 , #5 ; Logical shift right by 5 b i t s w i t h o u t f l a g s update
LSRS R7 , R5 , #5 ; Logical shift right by 5 b i t s with f l a g s update
B STOP
END ; End o f t h e program

Arithmetic Shift Right (ASR)

This instruction is similar to the signed divide by 2n where n number of shifts. Execute the
following instructions in your compiler and observe the result.
MOV R0 , #0x07
; Move 0 x07 t o l o w e r byte o f R0

MOV R3 , #0x669
; Move t h e 12 b i t s i g n e d hex number 669 t o t h e low p o r t i o n o f R3

MOV R5 , #0xD634
; Move t h e 16 b i t s i g n e d hex number D634 t o t h e low p o r t i o n o f R5

MOVT R5 , #0xFFB1
; Move t h e 16 b i t s i g n e d hex number D634 t o t h e h i g h p o r t i o n o f R5

STOP
ASR R4 , R3 , #5 ; Arithmetic shift right by 5 bits
ASR R2 , R3 , R0 ; Arithmetic shift right by t h e l o w e r byte v a l u e o f R0
ASR R6 , R5 , #5 ; Arithmetic shift right by 5 b i t s w i t h o u t f l a g s update
ASRS R7 , R5 , #5 ; Arithmetic shift right by 5 b i t s with f l a g s update
36 CHAPTER 4. ASSEMBLY LANGUAGE PROGRAMMING FUNDAMENTALS

B STOP
END ; End o f t h e program

Logical Shift Left (LSL)

Logical shift left instruction works fine for both signed and unsigned numbers. This instruction
is synonymous to multiply by 2n where n specifies the number of shifts.
MOV R7 , #0xC
; Move 0x0C t o l o w e r byte o f R7

MOV R0 , #0x281
; Move t h e 12 b i t s i g n e d hex number 281 t o t h e l o w e r p o r t i o n o f R0

MOV R2 , #0x25C3
; Move t h e 16 b i t s i g n e d hex v a l u e 25C3 t o t h e l o w e r p o r t i o n o f R2

MOV R4 , #0x593C
; Move t h e 16 b i t hex v a l u e 593C t o t h e low p o r t i o n o f R4

MOVT R4 , #0xA377
; Move t h e 16 b i t hex v a l u e A377 t o t h e h i g h p o r t i o n o f R4

STOP
LSL R1 , R0 , #3 ; Logical s h i f t l e f t by 3 b i t s
LSL R3 , R2 , R7 ; Logical s h i f t l e f t by t h e v a l u e i n t h e bottom byte o f
R7
LSL R5 , R4 , #10 ; Logical s h i f t l e f t by 10 b i t s w i t h o u t f l a g s update
LSLS R6 , R4 , #10 ; Logical s h i f t l e f t by 10 b i t s with f l a g s update
B STOP

END ; End o f t h e program

Rotate Right (ROR)

Bit-rotates do not discard any bits from the register. Instead, the bit values are removed from
one end of the register and inserted into the other end.
MOV R0 , #0x9
; Move 0 x09 t o l o w e r byte o f R0

MOV R1 , #0x25C3
; Move t h e 16 b i t s i g n e d hex v a l u e 25C3 t o t h e l o w e r p o r t i o n o f R2

MOV R3 , #0x73A2
; Move t h e 16 b i t hex v a l u e 73A2 t o t h e low p o r t i o n o f R3

MOVT R3 , #0x5D81
; Move t h e 16 b i t hex v a l u e 5D81 t o t h e low p o r t i o n o f R3
37

STOP

ROR R2 , R1 , #24 ; Rotate right l e f t by 24 b i t s


ROR R7 , R3 , R0 ; Rotate right by t h e v a l u e i n t h e bottom byte o f R0
ROR R4 , R3 , #10 ; Rotate right by 10 b i t s w i t h o u t f l a g s update
RORS R5 , R3 , #10 ; Rotate right by 10 b i t s with f l a g s update
B STOP
END ; End o f t h e program

Rotate Right Extended (RRX)

RRX is a ROR operation with a crucial difference. It rotates the number to the right by one
place but the original bit 31 is filled by the value of the Carry flag and the original bit 0 is
moved into the Carry flag. This allows a 33-bit rotation by using both the register and the
carry flag.
MOV R3 , #0xD129
; Move t h e 16 b i t hex v a l u e D129 t o t h e low p o r t i o n o f R3

MOVT R3 , #0xF29A
; Move t h e 16 b i t hex v a l u e F29A t o t h e low p o r t i o n o f R3
STOP
RRX R4 , R3 ; Rotate r i g h t e x t en d ed w i t h o u t f l a g s update
RRXS R6 , R3 ; Rotate r i g h t ex t e nd e d with f l a g s update
B STOP
END ; End o f t h e program

Writing an Assembly Program

Following is given the demo of assembly language program. We will learn how to write an
assembly language code from scratch but for now just use this code.
;;; Directives
PRESERVE8
THUMB

; V e c t o r Table Mapped t o Address 0 a t R e s e t


; Linker r e q u i r e s V e c t o r s t o be e x p o r t e d

AREA RESET, DATA, READONLY


EXPORT Vectors

Vectors
DCD 0 x20001000 ; s t a c k p o i n t e r v a l u e when s t a c k i s empty
DCD Reset Handler ; reset vector

ALIGN
38 CHAPTER 4. ASSEMBLY LANGUAGE PROGRAMMING FUNDAMENTALS

; The program
; Linker r e q u i r e s Reset Handler

AREA MYCODE, CODE, READONLY

ENTRY
EXPORT R e s e t H a n d l e r

Reset Handler
; ; ; ; ; ; ; ; ; ; User Code S t a r t s from t h e next l i n e ; ; ; ; ; ; ; ; ; ; ; ;

MOV R0 , #0x02
; Move t h e 8 b i t Hex number 02 t o t h e low p o r t i o n o f R0

MOV R2 , #0x1234
; Move t h e 16 b i t Hex number 1234 t o t h e low p o r t i o n o f R2

MOVT R2 , #0x1234
; Move t h e 16 b i t Hex number 1234 t o t h e h i g h p o r t i o n o f R2

STOP
LSL R0 , #1 ; Logical s h i f t l e f t by 1 b i t w i t h o u t f l a g s update
B STOP
END ; End o f t h e program

Exercises

1. R1 = 0x37AF. Apply LSR instruction on this value and store the result in R2. Consider
shift amounts as 1,3,12.

2. Assume R3 = 395A62. Apply ASR instruction and store the result in R4. Consider shift
amounts as 1,5,15.

3. R3 register contains a negative value -321. Apply the LSR and ASR instruction and
observe the result. Which instruction gives the correct result and why. Write your result
in both the cases and explain.

4. Suppose R0 register contains an unsigned hex value 0xA642. Multiply this hex value by
8 and write the result in R2.

5. Register R1 has signed value -458 and register R2 has unsingned value 458. Apply the LSL
operation on both the registers and observe the result. (Hint: Hexadecimal representation
of 458 is 0x1CA)

6. Compare the results of question 5 and explain why ASL is not needed.
39

7. What is the net result if a bit pattern (32 bits) is logical left shifted 2 positions and then
logical right shifted 2 positions?

8. Assume that R0 register contain 0xC5AF2. Using shift operation move the contents of
R0 to R4.

9. Now consider the value of R0 in previous as a signed value(use sign extension) and multiply
it by 8 and observe the result

10. Move hexadecimal value 0xA964 to the register R1 and observe the result of each of LSR,
ASR, LSL, ROR and RRX instructions. Assume the shift amount equal to 1. State your
findings.

11. Move the hexadecimal value 0xD29D1C8B to register R5 and apply the operations LSR,
ASR, LSL, ROR and RRX with flags update. Write your results.

12. Rotate the contents of R3 register to left by 12 bits. Assume R3 = 0x568A1FC3.

13. Consider R5 = 0x65FF. Move this value to the higher 16 bits using only one instruction.

14. Consider R6 = 0x76543210. Swap the top and bottom halves of R6.

15. Perform a RRX operation on R6 register and observe the result.

Você também pode gostar