MODULE 1 Updated
MODULE 1 Updated
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
3
RECOMMENED TEXT
• K. Irvine, Assembly Language for x86 Processors, 7th ed. Pearson, 2015.
INTRODUCTION TO ASSEMBLY
LANGUAGE
&
BASIC CONCEPTS
MODULE 1
INTRODUCTION
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.
• 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.)
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.
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
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:
25
DATA REPRESENTATION(CONTD.)
Binary Integers(Contd.)
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
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 :
31
Examples
• 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
• 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?
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.
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
DATA REPRESENTATION (CONTD.)
Converting Signed Decimal to Hexadecimal
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.
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)
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.)
55
Boolean Expressions (Contd.)
• NOT • AND
• The NOT operation reverses a Boolean value. • The Boolean AND operation requires two operands.
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
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.
60
DATA REPRESENTATION (CONTD.)
Truth Tables for Boolean Functions
• A boolean function receives boolean inputs • Example: X ∧ ¬Y
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