CA 08 Assembly Language Programming Copy (1)
CA 08 Assembly Language Programming Copy (1)
1
High and Low Level Languages
• C, C++, Java, Basic and the likes are all high level languages.
• They are machine independent.
• Assembler language is a low level language.
• Each different computer architecture has its own assembler language.
• The machine dependent compilers translate the high level language
to machine language.
2
Microcode
• Assembler or machine language is the lowest level access a
programmer has to the hardware.
• Internally, the machine language is implemented with microcode.
• Microcode is a series of instruction words that turn switches on and
off to implement each machine language instruction.
3
Compilers
• A compiler translates a high level language, such as C or C++, into
machine language.
• Each line of a high level language usually produces many machine
instructions.
• Compilers can rearrange the instructions to produce optimal code.
4
Compiled Results
5
Architecture Specific
• The same C++, Fortran, Pascal or Java source code can be compiled on
any architecture.
• When executed, it will give the same results.
• Each architecture has its own assembler language.
• Assembler for one type of machine will not run on another machine.
• Assembler language is a simplified way of writing machine language.
6
Writing Assembler
• You need an assembler program that will translate your assembler
source into machine language.
• You can use the Inline Assembler in Microsoft Visual Studio to embed
assembly-language instructions directly in your C and C++ source
programs.
__asm {
assembler code here
}
• More details on using inline assembler are at:
• https://docs.microsoft.com/en-us/cpp/assembler/inline/inline-assembler
7
8
8
Registers
• Registers are high speed, temporary storage in the processor.
• Some registers you can manipulate directly with assembler.
• The number of registers varies with the architecture.
• The Pentium has 8. IBM mainframes have 16, Itanium has 32.
• In some architectures, all registers are the same. In others, registers
are specialized.
9
Registers Do Everything
• All data moves through the registers
• Register to register instructions
• Memory to register instructions
• Memory to memory instructions (rare).
• Registers can hold addresses.
• Instructions accessing data in memory, can use an index register to
specify the address
10
Changing Names
• The first IBM PC had an Intel 8088 with 16 bit registers.
• The registers were named AX, BX, etc.
• When Intel extended the processor to 32 bit registers, they called the
longer registers EAX, EBX, etc.
• AX is the lower 16 bits of EAX.
• AH and AL are the high and low byte of the 16 bit register, now bytes
3 & 4.
11
Intel Registers
• The Intel Pentium has eight 32-bit general- purpose registers
12
General or Specialized
• In some architectures all of the registers have the same functionality.
In other machines the registers each have a specific purpose.
• The Intel registers have special purposes, although most can do
several operations.
• EAX Accumulator for arithmetic
• EBX Pointer to data
• ECX Counter for loop operations
• EDX I/O pointer
• ESP Stack pointer
13
Load and Store
• A Load instruction copies a value from memory into a register.
• (Reads memory)
• A Store instruction copies a value from a register into memory.
• (Writes memory)
• mov for both load and store.
14
mov Instruction
• The mov instruction moves data between memory and a register or
between two registers.
• The format is,
mov destination, source
• where destination and source can be
• register, memory to load data into a register
• memory, register to store data into memory
• register, register to move data between regs
15
Assignment Statements
16
Data Types
• Hardware Data Types • Software Data Types
• long, int and short • All other data types are created by
• (8, 4 & 2 bytes) software
• float and double • strings
• (4 & 8 bytes) • objects
• char or byte • boolean
• (1 byte) • multi-dimensional arrays
• Integer data types can be signed
or unsigned
17
Arithmetic and Logical Instructions
18
Arithmetic Example 1
19
Arithmetic Example 2
20
What value is in EAX at the end?
21
Increment and Decrement
• The inc and dec instructions are one of the few that can run on
memory locations without using the registers.
• You can increment or decrement the value in a register or memory
location
inc eax
dec memoryAddr
22
Big Operands
• Multiplication and Division use two registers to store a 64 bit value.
• A number is stored in EDX:EAX with the most significant bits in the
EDX register and the least significant bits in EAX.
23
Multiplication
• The imul signed multiply instruction has three forms.
• Multiply memory * EAX .
imul memory
• Multiply memory * register.
imul reg, memory
• Multiply the value in the memory location times the constant and
store the result in the register .
imul reg, memory, const
24
Division
• The 64 bit number in the EDX:EAX pair of registers is divided by the
32 bit value in a memory location or another register.
• The resulting quotient is stored in EAX
• The resulting remainder is stored in EDX
• Since the EDX:EAX registers are always used, you do not have to
specify them.
idiv memoryAddr
25
Arithmetic Examples 3 & 4
26
Shifts
• The shift instructions can shift the values in a register or memory
location.
• The SHR and SHL instructions shift the bits right or left by the
specified number of bits.
• The SAR and SAL instructions shift the bit right or left, but not the
sign bit.
• The shift count can be a constant or the cl reg.
• sar eax, 5
• shl eax, cl
27
Shift Example
28
Learn Assembly Language Programming
• Assembly Programming Tutorial
• https://www.tutorialspoint.com/assembly_programming/index.htm
• Compile and Execute Assembly Online
• https://www.tutorialspoint.com/compile_assembly_online.php
• Online Assembler - NASM Compiler IDE
• https://www.jdoodle.com/compile-assembler-nasm-online/
29
Next:
Multicores, Multiprocessors, and Clusters
30