Download - [ASM] Lab1
1
Assembly Language - Lab (1)
Agenda Logistics CourseSites Introduction Data Representation
2
Logistics
Text Book
3
Evaluation
Year Work (25 Marks)Assignments Lab workProject
4
AssignmentsAssignments are INDIVIDUAL work. Never share code/solution.
5
Honor CodeMy answers will be my own work.I will not make solutions available or seen by anyone else.
Violations:Plagiarism (copy all or part of it)Representing the work of another as one’s own work
6
CourseSiteshttps://www.coursesites.com/
9
Introduction
Why Assembly?All high-level languages are an abstraction how the
computer works, abstraction means the programmer don’t have to worry about the computer details.
Assembly is a really good way to understand what is the computer doing because you control exactly what happens at each step.
10
Why Assembly?Assembly language gives the programmer the ability to
perform technical tasks that would be difficult in high‐level languages including total control on the machine.
Software written in assembly language runs faster than the same one written in high level language and takes ‐less amount of memory if the programmer well‐optimized the assembly program code.
11
Why Assembly?Learning assembly language gives deep understanding of
the computer’s organization and architecture and how programs run, since it is necessary to know the architecture of the processor or controller in order to write assembly code.
12
What can we do using Assembly?Device Driver:
is a program that controls a particular type of device that is attached to your computer.
Only assembly and C can implement this since they give you a full control over the hardware.
13
What can we do using Assembly?Virus Programming:
a simple program that infects other programs:1. by injecting itself in the end of the program2. by applying changes to file header and RPT (Relocation pointer table) to execute the virus first and execute the host program
14
What can we do using Assembly?Reverse Engineering:
is the process of reversing code from a machine language (binary code) using disassembler, then we can:analyze and understand the programchanging features in program (ex: cracking the
program)debugging program
without having the source code of the program
15
What can we do using Assembly?Embedded Software:
a software written to control a machine or device, that is specialized for a certain device, and has time and memory constraints, such as telephone, automobile, air-condition control system, video cards, sound cards, printers, etc.
Since assembly is the fastest language and takes the lowest memory, it is the best for embedded system.
16
17
Machine Language VS Assembly Language
Machine LanguageComputers work only with 0’s and 1’s.
Every program instruction or data element must be in binary to be manipulated by computer machine.
Therefore, any program understood by machine has to be written in machine language, however machine language is too hard to write and maintain.
18
Machine LanguageMachine Language is a set of binary codes (0’s and 1’s)
that represent instructions of a specific machine. It is machine dependent.‐
For example, the instruction8B D8
means copy content from AX register to BX register.
19
Assembly LanguageAssembly language is developed to make programming
easier than programming using machine language.
Assembly language is a set of mnemonics (symbols) for machine code instructions plus other features that make programming easier.
20
Assembly LanguageTo run program written in assembly language, we should
have a converter (or translator) which converts these labels and mnemonics to their corresponding machine codes in 0’s and 1’s. This converter is called assembler.
21
Machine CodeAssemblerAssembly Code
Assembly LanguageAssembly Language is a low-level (machine level) programming ‐
language that uses mnemonics instead of numeric codes to simplify programming.
For example, the instructionmov BX, AX means copy content from AX register to BX register.
Each statement in assembly code has a one-to-one relationship with machine language instructions, in other words each statement corresponds to a single machine code instruction.
22
Assembly Language
Each assembly language is machine dependent which ‐means it is specific to a particular computer architecture.
In contrast to most high-level programming languages, which are generally portable across multiple systems.
23
Assembly LanguageWe’ll use Irvine Library with Visual Studio to write our
Assembly code.
24
25
Data Representation Numbering Systems Conversions
Numbering SystemsData Representation
26
System Base Possible Digits
Binary 2 0 and 1
Octal 8 0, 1, 2, 3, 4, 5, 6, and 7
Decimal 10 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9
Hexadecimal 160, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E,
and F
Converting from unsigned binary to decimal
1101102
1x25 + 1x24 + 0x23 + 1x22 + 1x21 + 0x20
= 1x32 + 1x16 + 0 + 1x4 + 1x2 + 0= 32 + 16 + 4 + 2= 5410
27
Converting from unsigned binary to decimal
111100002
1x27 + 1x26 + 1x25 + 1x24 + 0x23 + 0x22 + 0x21 + 0x20
= 1x128 + 1x64 + 1x32 + 1x16 + 0 + 0 + 0 + 0= 128 + 64 + 32 + 16= 24010
28
Converting from signed binary to decimal
001010102
0x26 + 1x25 + 0x24 + 1x23 + 0x22 + 1x21 + 0x20
= 0 + 1x32 + 0 + 1x8 + 0 + 1x2 + 0= 32 + 8 + 2= +4210
29
1st bit is 0, then the number is positive
Converting from signed binary to decimal
111100002
Get the 2’s complement2’s complement = 1’s complement + 100010000 = 00001111 + 1
Convert the 2’s complement to decimal and attach the negative sign 0x27 + 0x26 + 0x25 + 1x24 + 0x23 + 0x22 + 0x21 + 0x20
= 0 + 0 + 0 + 16 + 0 + 0 + 0 + 0= 16= -1610
30
1st bit is 1, then the number is negative
• 2310
= 101112
Division Quotient Remainder23 / 2 =
11.5 11 1
11 / 2 = 5.5 5 15 / 2 = 2.5 2 12 / 2 = 1 1 0
1 / 2 = 0.5 0 1
10111
Stop when quotient = 0
31Converting from unsigned decimal to binary
• -2310
a. Convert the decimal value into binaryDivision Quotient Remainder
23 / 2 = 11.5 11 1
11 / 2 = 5.5 5 1
5 / 2 = 2.5 2 1
2 / 2 = 1 1 0
1 / 2 = 0.5 0 1
10111
Stop when quotient = 0
32
Converting from signed decimal to binary
b. If the original number is negative, then get the 2’s complement of the resultResult = 101112 = 000101112
2’s complement = 1’s complement + 1 = 11101000 + 1 = 111010012
33
Converting from signed decimal to binary
Converting from unsigned binary to hexadecimal
Each hexadecimal digit corresponds to 4 binary bits. 0101 10112
Convert each 4 bits to a hexadecimal digit
=5B16
34
0 x 23 + 1 x 22 + 0 x 21 + 1 x 20 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20
0 + 1 x 4 + 0 + 1 x 1 1 x 8 + 0 + 1 x 2 + 1 x 1
4 + 1 8 + 2 + 1
5 11
5 B
35
• A616
a. Convert each hexadecimal digit to 4 bits
= 101001102
A = 10Division Quotient Remainder
10 / 2 = 5 5 05 / 2 = 2.5 2 12 / 2 = 1 1 0
1 / 2 = 0.5 0 1
1010
6Division Quotient Remainder
6 / 2 = 3 3 03 / 2 = 1.5 1 11 / 2 = 0.5 0 1
… … 0
0110
Converting from unsigned hexadecimal to binary
36
• Addition and Subtraction• 1’s Complement
• Covert each 1 to 0, and each 0 to 1.1’s complement of 10110102 = 01001012
• 2’s Complement• Add 1 to the 1’s complement.
2’s complement of 10110102 = 01001102
• 2’s complement is used in representing negative numbers.
Numbering Systems
37
• Binary Operations• 11001 + 10101 = 101110• The operation’s result does not fit in 5 bits, so the underlined
1 in the previous number is called a carry.
• 11001 – 10101 = 11001 + 01011 = 00100 with carry = 1• Carry = 1 in subtraction means that the result is positive with no
borrow.
• 10101 – 11001 = 10101 + 00111 = 11100 with carry = 0• Carry = 0 in subtraction means that the result is negative with
borrow.
2’s complement of 10101
2’s complement of 11001
Numbering Systems
38
• Hexadecimal Operations• 23D9 + 94BE
9 + 14 = 23 23 – 16 = 7 with carry13 + 11 + 1 = 25 25 – 16 = 9 with carry3 + 4 + 1 = 82 + 9 = B= B897
• 59F – 2B815 – 8 = 7(9 + 16) – 11 = 14 (E)4 – 2 = 2= 2E7
Numbering Systems
Additional Examples1. 000101101010011110010100)2 = )16
2. 1234)16 = )10
3. 422)10 = )16
4. 3628286A + 4245584B )16 = )16
5. C675 – A247)16 = )16
39
• All data stored in memory is numeric.
• Characters are stored by using a character code that maps numbers to characters.
• One of the most common character codes is known as ASCII (American Standard Code for Information Interchange). It uses 1 byte (8 bits) to encode characters. Therefore, it is limited to encode only 256 (28) characters.
• A new and more complete code that is supplanting ASCII is Unicode. It uses 2 bytes (16 bits) to encode characters. Therefore, it is capable to encode 65536 (216) characters.
ASCII Code40
41
Questions !?
42
Thank you