0% found this document useful (0 votes)
35 views64 pages

MODULE 1 Updated

This document provides an overview of an assembly language programming course. The key points are: - The course will teach students basic assembly language programming and computer architecture principles as applied to x86 processors. - Students will learn how to create assembly language applications and understand how memory, addresses, and instructions work at a low level. - The required tools are a computer running Windows and Microsoft Visual Studio. An assembly language textbook is recommended. - Assembly language has a direct correspondence to machine language and provides direct access to hardware.

Uploaded by

Godwin Ekanem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views64 pages

MODULE 1 Updated

This document provides an overview of an assembly language programming course. The key points are: - The course will teach students basic assembly language programming and computer architecture principles as applied to x86 processors. - Students will learn how to create assembly language applications and understand how memory, addresses, and instructions work at a low level. - The required tools are a computer running Windows and Microsoft Visual Studio. An assembly language textbook is recommended. - Assembly language has a direct correspondence to machine language and provides direct access to hardware.

Uploaded by

Godwin Ekanem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

ASSEMBLY LANGUAGE

PROGRAMMING
(CEN416)
INSTRUCTORS:
DR. MOSES OLANIYAN
&
ENGR. TIWALADE ODU
LEARNING OUTCOMES

• To understand the basic principles of computer architecture, machine language and low-level
programming as applicable to x86 processors.
• To understand basic Boolean logic and how it applies to programming and computer hardware.
• To understand how x86 processors manage memory with the protected and virtual modes.
• To learn how to create assembly language application programs.
• To understand how memory, address and instructions work at a low level.

2
REQUIRED TOOLS

• A computer that runs a 32-bit or 64-bit version of Microsoft Windows.


• Microsoft Visual Studio 2019.

3
RECOMMENED TEXT

• K. Irvine, Assembly Language for x86 Processors, 7th ed. Pearson, 2015.
INTRODUCTION TO ASSEMBLY
LANGUAGE
&
BASIC CONCEPTS
MODULE 1
INTRODUCTION

• Basic concepts of assembly language.


• Data representations.
• Boolean operations.
INTRODUCTION (CONTD.)

• Assembly language is the oldest programming language.


• It has the closest resemblance to native machine language.
• It provides direct access to computer hardware thereby requiring one to understand the
computer’s architecture and operating system.

7
INTRODUCTION (CONTD.)
• Assembler: a utility program that converts source code programs from assembly language into
machine language.
• MASM(Microsoft Macro Assembler) will be used for this course.

• Linker: a utility program that combines individual files created by an assembler into a single
executable program.
• A debugger lets you step through a program while it is running and examine registers and
memory.

8
WHAT TYPES OF PROGRAMS CAN BE CREATED USING MASM?

• 32-Bit Protected Mode: These programs run under all 32-bit versions of Microsoft Windows.
• They are usually easier to write and understand than real-mode programs.
• They can be simply called 32-bit mode.

• 64-Bit Mode: These programs run under all 64-bit versions of Microsoft Windows.
• 16-Bit Real-Address Mode: These programs run under 32-bit versions of Windows and on
embedded systems.
• They are not supported by the 64-bit version of Windows.

9
HOW DOES ASSEMBLY LANGUAGE RELATE TO MACHINE LANGUAGE?
• Machine language is a numeric language specifically understood by a computer’s processor (the
CPU).
• All x86 processors understand a common machine language.
• Assembly language consists of statements written with short mnemonics such as ADD, MOV, SUB,
and CALL.
• Assembly language has a one-to-one relationship with machine language: Each assembly
language instruction corresponds to asingle machine-language instruction.

10
INTRODUCTION (CONTD.)
How Do C++ and Java Relate to Assembly Language?

• High-level languages such as Python, C++, and Java have a one-to-many relationship with
assembly language and machine language.
• A single statement in C++, for example, expands into multiple assembly language or machine
instructions.

11
How C++ Relates to Assembly Language
English: Display the sum of A times B plus C.

C++: cout << (A * B + C);

Assembly Language: Intel Machine Language:


mov eax,A A1 00000000
mul B F7 25 00000004
add eax,C
03 05 00000008
call WriteInt
E8 00500000

Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2014. 12


INTRODUCTION (CONTD.)

• Assembly language is not portable because it is designed for a specific processor family.
• Examples of processor families are Motorola 68x00, SUN Sparc, Vax and IBM-370.
• Assembly language is an ideal tool for writing embedded programs because of its economical use of
memory.
• Assembly language permits you to precisely specify a program’s executable code in real-time
applications that deal with simulation and hardware monitoring and require precise timing and
responses.
• Computer game consoles require their software to be highly optimized for small code size and fast
execution.
• Game programmers use assembly language as their tool of choice because it permits direct access to computer
hardware, and code can be hand optimized for speed.

13
COMPARISON OF ASSEMBLY LANGUAGE TO HIGH-LEVEL LANGUAGES

14
VIRTUAL MACHINE CONCEPT
• It effectively demonstrate the relationship between a computer’s hardware and software.
• A computer can usually execute programs written in its native machine language. Each instruction
in this language is simple enough to be executed using a relatively small number of electronic
circuits. For simplicity, we will call this language L0.
• Programmers would have a difficult time writing programs in L0 because it is enormously detailed
and consists purely of numbers. If a new language, L1, could be constructed that was easier to
use, programs could be written in L1. There are two ways to achieve this:
• Interpretation: As the L1 program is running, each of its instructions could be decoded and executed by a
program written in language L0. The L1 program begins running immediately, but each instruction has to
be decoded before it can execute.
• Translation: The entire L1 program could be converted into an L0 program by an L0 program specifically
designed for this purpose. Then the resulting L0 program could be executed directly on the computer
hardware.

15
VIRTUAL MACHINE CONCEPT (CONTD.)

• A virtual machine as a software program that


emulates the functions of some other physical or
virtual computer.
• Virtual machine, VM1 can execute commands written
in language L1.
• Also, virtual machine, VM0 can execute commands
written in language L0.

16
VIRTUAL MACHINE CONCEPT (CONTD.)
• If the language VM1 supports is not programmer-friendly enough to be used for useful
applications, another virtual machine, VM2, can be designed that is more easily understood.
• This process can be repeated until a virtual machine, 𝑽𝑴𝒏 can be designed to support a
powerful, easy-to-use language.
• Java is based on the virtual machine concept.
• A program written in the Java language is translated by a Java compiler into Java byte code.
• Java byte code is a low-level language that is executed quickly at runtime by a program known as a
Java virtual machine (JVM).
• JVM has been implemented on different computer systems thus making Java programs relatively system
independent.

17
VIRTUAL MACHINE CONCEPT (CONTD.)
Virtual Machine Levels
• Relating the virtual machine concept to actual computers and languages:
• The computer’s digital logic hardware represents machine Level 1.

• Instruction Set Architecture(ISA) represents Level 2.


• This is the first level at which users can typically write programs, although the programs consist of binary
values called machine language.
• Machine language refers to the instructions designed into the processor to carry out basic operations,
such as add, subtract and move.

• Assembly Language represents Level 3.


• It uses short mnemonics such as ADD, SUB, and MOV, which are easily translated to the ISA level.
• Instruction mnemonics have a one-to- one correspondence to machine language.

• High-Level Languages represent Level 4.


• Application-oriented languages such as C, C++, Java, Pascal, and Visual Basic.
• Programs compile into assembly language.

18
DATA REPRESENTATION
• The memory contents of a computer are described with binary, decimal and hexadecimal numbers.
• Each numbering format, or system, has a base, or maximum number of symbols that can be assigned to
a single digit.

19
DATA REPRESENTATION(CONTD.)
Binary Integers
• A computer stores instructions and data in memory as collections of electronic charges.
• To represent these entities with numbers, a system that uses the concept of on and off or true and false
should be employed.
• Binary numbers are base 2 numbers, in which each binary digit (called a bit) is either 0 or 1.
• Bits are numbered sequentially starting at zero on the right side and increasing toward the left.
• The bit on the left is called the most significant bit (MSB), and the bit on the right is the least
significant bit (LSB).

20
DATA REPRESENTATION(CONTD.)
Binary Integers(Contd.)
• Binary integers can be signed or unsigned.
• A signed integer can be positive or negative.
• An unsigned integer is by default positive.
• Zero is considered positive.
• Unsigned Binary Integers
• Starting with the LSB, each bit in an unsigned binary integer represents an increasing power of 2.

21
DATA REPRESENTATION(CONTD.)
Binary Integers(Contd.)

22
DATA REPRESENTATION(CONTD.)
Binary Integers(Contd.)
• Converting Unsigned Binary Integers to Decimal
Weighted positional notation shows how to calculate the decimal value of
each binary bit:
dec = (Dn-1  2n-1) + (Dn-2  2n-2) + ... + (D1  21) + (D0  20)
D = binary digit

binary 00001001 = decimal 9:


(1  23) + (1  20) = 9

23
DATA REPRESENTATION(CONTD.)
Binary Integers(Contd.)
• Converting Unsigned Decimal Integers to Binary
• Repeatedly divide the decimal integer by 2. Each
remainder is a binary digit in the translated value.
• Example: Convert 37 to binary
• The remainder digits, starting from the top
row, are the binary digits D0, D1, D2, D3, D4,
and D5.
• Concatenate the binary bits from the
remainder column of the table in reverse
order (D5, D4, . . .) to produce binary
100101.
• Fill the remaining two-digit positions on the
left with zeros, producing 00100101 because
computer storage always consists of binary
numbers whose lengths are multiples of 8.

24
DATA REPRESENTATION(CONTD.)
Binary Integers(Contd.)
• Binary Addition
• When adding two binary integers, proceed bit by bit, starting with the LSB.
• There are four ways to add two binary digits:

• Example: Add 00000100 and 00000111

25
DATA REPRESENTATION(CONTD.)
Binary Integers(Contd.)

• Binary Addition (Contd.)


• When a carry is generated out of the MSB, the size of the storage location becomes important.

• Example: 𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟏 + 𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟏 = 𝟏𝟎𝟎𝟎𝟎𝟎𝟎𝟎𝟎


• If the storage location for the sum is at least 9 bits long, we can represent the sum as 100000000. But if the sum can
only store 8 bits, it will equal to 00000000, the lowest 8 bits of the calculated value.

26
DATA REPRESENTATION(CONTD.)
Binary Integers(Contd.)
• Integer Storage Sizes
• The basic storage unit for all data in an x86 computer is a byte.

• 𝟏 𝒃𝒚𝒕𝒆(𝑩) = 𝟖 𝒃𝒊𝒕𝒔

• 𝟏 𝒘𝒐𝒓𝒅(𝑾) = 𝟐 𝒃𝒚𝒕𝒆𝒔 = 𝟏𝟔 𝒃𝒊𝒕𝒔

• 𝟏 𝒅𝒐𝒖𝒃𝒍𝒆𝒘𝒐𝒓𝒅(𝑫𝑾) = 𝟒 𝒃𝒚𝒕𝒆𝒔 = 𝟑𝟐 𝒃𝒊𝒕𝒔

• 𝟏 𝒒𝒖𝒂𝒅𝒘𝒐𝒓𝒅(𝑸𝑾) = 𝟖 𝒃𝒚𝒕𝒆𝒔 = 𝟔𝟒 𝒃𝒊𝒕𝒔

• 𝟏 𝒅𝒐𝒖𝒃𝒍𝒆 𝒒𝒖𝒂𝒅𝒘𝒐𝒓𝒅(𝑫𝑸𝑾) = 𝟏𝟔 𝒃𝒚𝒕𝒆𝒔 = 𝟏𝟐𝟖 𝒃𝒊𝒕𝒔

27
DATA REPRESENTATION(CONTD.)
Binary Integers(Contd.)
• Ranges and Sizes of Unsigned Integer Types

28
DATA REPRESENTATION(CONTD.)
Hexadecimal Integers

• They offer a convenient representation of large


binary data which otherwise would have been
cumbersome to read.
• 1 hexadecimal digit represents 4 binary bits.
• 2 hexadecimal digits represent 1 byte.
• Example: Translate the binary integer
000101101010011110010100 to hexadecimal.

29
Translate the binary integer 000101101010011110010100 to hexadecimal:

30
DATA REPRESENTATION(CONTD.)
Hexadecimal Integers(Contd.)
• Converting Unsigned Hexadecimal to Decimal
• In hexadecimal, each digit position represents a power of 16.
• This is helpful when calculating the decimal value of a hexadecimal integer.
• If the digits in a four-digit hexadecimal integer are numbered with subscritps as 𝑫𝟑 𝑫𝟐 𝑫𝟏 𝑫𝟎 .
• The decimal value of the hexadecimal integer is calculated by multiplying each digit by its corresponding
power of 16 :

dec = (D3  163) + (D2  162) + (D1  161) + (D0  160)


• The decimal value of any n-digit hexadecimal integer can be calculated thus:

dec = (Dn-1  16n-1) + (Dn-2  16n-2)+ … + (D1  161) + (D0  160)

31
Examples

• 1234H = (1  163) + (2  162) + (3  161) + (4  160), or decimal 4,660.

• 3BA4H = ? decimal

32
• 3BA4H = (3  163) + (11  162) + (10  161) + (4  160), or
decimal 15,268.

33
• Powers of 16
• They are used when calculating hexadecimal values up to 8 digits long:

34
• Converting Unsigned Decimal to Hexadecimal

35
Hexadecimal Addition
• Debugging utility programs (known as debuggers) usually display memory
addresses in hexadecimal.
• Two addresses are often added to locate a new address.

36
• Divide the sum of two digits by the number base (16). The quotient
becomes the carry value, and the remainder is the sum digit.
1 1
36 28 28 6A
42 45 58 4B
78 6D 80 B5

21 / 16 = 1, rem 5

Important skill: Programmers frequently add and subtract the


addresses of variables and instructions.
38

• When a borrow is required from the digit to the left, add 16 (decimal) to the current
digit's value:
16 + 5 = 21

−1
C6 75
A2 47
24 2E

Practice: The address of var1 is 00400020. The address of the next variable after var1 is
0040006A. How many bytes are used by var1?

Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2014. 38


DATA REPRESENTATION (CONTD.)
Signed Binary Integers
• Signed binary integers are positive
or negative.
• For x86 processors, the MSB indicates
the sign.
• 0 is positive and 1 is negative.

39
DATA REPRESENTATION (CONTD.)
Two’s-Complement Representation
• Negative integers are stored in two's
complement notation.
• The two’s complement of an integer is its
additive inverse.
• Two’s-complement representation is useful
to processor designers because it removes
the need for separate digital circuits to • The two’s complement of a binary integer is
handle both addition and subtraction. formed by inverting (complementing) its bits
• For example, if presented with the and adding 1.
expression A - B, the processor can simply • The two’s-complement operation is
convert it to an addition expression:
A + (-B). • reversible,
40
DATA REPRESENTATION (CONTD.)
Hexadecimal Two’s Complement
• To create the two’s complement of a hexadecimal integer, reverse all bits and
add 1.
• The reversal of the bits can be done by subtracting the digit from 15.

6A3D --> 95C2 + 1 --> 95C3

95C3 --> 6A3C + 1 --> 6A3D

41
DATA REPRESENTATION (CONTD.)
Algorithm for Converting Signed Binary to Decimal
• If the highest bit is a 1, the number is stored
in two’s-complement notation. Create its
two’s complement a second time to get its
positive equivalent. Then convert this new
number to decimal as if it were an unsigned
binary integer.
• If the highest bit is a 0, you can convert it to
decimal as if it were an unsigned binary • Because the original integer (11110000)
integer. was negative, we know that its decimal
value is −16.

42
DATA REPRESENTATION (CONTD.)
Converting Signed Decimal to Binary
• Convert the absolute value of the decimal • To convert -43 decimal to binary:
integer to binary. Starting Value -43

• If the original decimal integer was negative, Step 1:Convert the 00101011
absolute value of -43
create the two’s complement of the binary (which is 43) to binary
number from the previous step.
Step 2: Create the two’s 11010101
complement of
00101011

• -43 decimal = 11010101

43
DATA REPRESENTATION (CONTD.)
Converting Signed Decimal to Hexadecimal

• Convert the absolute value of the decimal integer to


hexadecimal.
• If the decimal integer was negative, create the two’s
complement of the hexadecimal number from the previous
step.

44
DATA REPRESENTATION (CONTD.)
Converting Signed Hexadecimal to Decimal
• If the hexadecimal integer is negative, create its two’s complement;
otherwise, retain the integer as is.
• Convert the result of the previous step to decimal. If the original
value was negative, attach a minus sign to the beginning of the
decimal integer.

If the highest digit of a hexadecimal integer is > 7, the value is negative.


Examples: 8A, C5, A2, 9D

45
DATA REPRESENTATION (CONTD.)
Maximum and Minimum Values
• A signed integer of n bits uses only n - 1 bits to represent the number’s
magnitude.

46
DATA REPRESENTATION (CONTD.)
Binary Subtraction

• A simple way to approach binary subtraction is to reverse the sign of the value
being subtracted, and then add the two values.
• This method requires you to have an extra empty bit to hold the number’s sign if
a larger unsigned binary integer is being subtracted from a small one.
• If a smaller unsigned binary integer is being subtracted from a large one, the
carry out of the MSB of the result of the subtraction should be ignored.

47
Example: Subtract 00000011(3) from 00001100 (12)

00001100 (A) 00001100 (A)


(B)
+
- 00000011 11111101 (-B)
1 00001001 (9)

Practice: Subtract 0101 from 1001.

48
DATA REPRESENTATION (CONTD.)
Character Storage
• Computers only store binary data … but how are • ANSI (American National Standards Institute)
characters represented? Character Set
• It defines an 8-bit character set that represents up to
• Computers are represented with a character set. 256 characters
• The character set is a mapping of characters to • The first 128 characters correspond to the letters and
integers. symbols on a standard U.S. keyboard.
• The second 128 characters represent special characters
• ASCII (American Standard Code for Information such as letters in international alphabets, accents,
Interchange) Character Set currency symbols, and fractions.
• Assigns a unique 7-bit integer to each character.
• ASCII codes use only the lower 7 bits of every byte, the
extra bit is used to create a proprietary character set
on different computers.

49
Character Storage
• Unicode Standard • ASCII Strings
• It was created as a universal way of defining • A sequence of one or more characters is
characters and symbols.
called a string.
• It defines numeric codes (code points) for
characters, symbols, and punctuation used in all • an ASCII string is stored in memory as a
major languages. succession of bytes containing ASCII codes.
• Three transformation formats are used to • e.g., the numeric codes for the string “ABC123”
transform code points into displayable characters: are 41h, 42h, 43h, 31h, 32h, and 33h.
• UTF-8 is used in HTML and has the same byte values
as ASCII. • A null-terminated string is a string of
• UTF-16 is used in environments that balance efficient characters followed by a single byte
access to characters with economical use of storage. containing zero.
• Each character is encoded in 16 bits.
• UTF-32 is used in environments where space is no
concern and fixed-width characters are required.
Each character is encoded in 32 bits.
50
• Using the ASCII Table
• To find the hexadecimal ASCII code of a character, look This is shown as follows in simplified form:
along the top row of the table and find the column
containing the character you want to translate.
• The most significant digit of the hexadecimal value is in
the second row at the top of the table; the least
significant digit is in the second column from the left.
• For example, to find the ASCII code of the letter a, find
the column containing the a and look in the second row:
The first hexadecimal digit is 6.
• Next, look to the left along the row containing a and
note that the second column contains the digit 1.
• Therefore, the ASCII code of a is 61 hexadecimal.

51
ASCII TABLE

52
DATA REPRESENTATION (CONTD.)
Numeric Data Representation
• A number’s interpretation can depend on the context in which it appears.
• It is therefore important to use precise terminology when describing the way numbers and
characters are represented in memory and on the display screen.
• Decimal 65
• is stored in memory as a single binary byte as 01000001.
• a debugging program would probably display the byte as “41,” which is the number’s hexadecimal
representation.
• If the byte were copied to video memory, the letter “A” would appear on the screen because 01000001
is the ASCII code for the letter A.

53
Numeric Data Representation (Contd.)

• A specific name is assigned to each type of data


representation.
• A binary integer is an integer stored in memory in its
raw format, ready to be used in a calculation.
• Binary integers are stored in multiples of 8 bits
(such as 8, 16, 32, or 64).
• A digit string is a string of ASCII characters, such as
“123” or “65.”
• This is simply a representation of the number and
can be in any of the formats shown for the decimal
number 65
54
DATA REPRESENTATION (CONTD.)
Boolean Expressions
• Boolean algebra defines a set of operations on the
values true and false.
• It was invented by George Boole.
• Boolean algebra can be used to describe the design of
digital circuits.
• Boolean expressions are used in computer programs to
express logical operations.
• A Boolean expression involves a Boolean operator and
one or more operands.
• The set of operators includes the following:
• NOT: represented as ¬ or ~ or ’
• AND: represented as ∧ or •
• OR: represented as ∨ or +

55
Boolean Expressions (Contd.)
• NOT • AND
• The NOT operation reverses a Boolean value. • The Boolean AND operation requires two operands.

Digital gate diagram for AND

• The output is true only when both inputs are true.


Digital gate diagram for NOT

56
Boolean Expressions (Contd.)
• AND (Contd.)
• In assembly language, the AND operation is bitwise.
• Example:
X: 11111111
Y: 00011100
X ∧ Y: ?

57
Boolean Expressions (Contd.)
• OR
• The Boolean OR operation requires two operands

Digital gate diagram for OR

• The output is false only when both inputs are false.

58
Boolean Expressions (Contd.)
• OR (Contd.)
• The OR operation is bitwise.
• Example:
X: 11111111
Y: 00011100
X ∨ Y: ?

59
Boolean Expressions (Contd.)

• Operator Precedence
• Operator precedence rules are used to indicate
which operators execute first in expressions
involving multiple operators.

• The NOT operator has the highest precedence,


followed by AND and OR.

60
DATA REPRESENTATION (CONTD.)
Truth Tables for Boolean Functions
• A boolean function receives boolean inputs • Example: X ∧ ¬Y

and produces a boolean output.

• A truth table can be constructed for any


boolean function, showing all possible inputs
and outputs.

61
Truth Tables for Boolean Functions (Contd.)
• Example: ¬X ∨ Y

62
Truth Tables for Boolean Functions (Contd.)
• Example: (Y ∧ S) ∨ (X ∧ ¬S)

63
54 68 65 20 45 6E 64
What do these numbers represent?

64

You might also like