Assembly Language Basics
Motivation
Discuss the basics to an assembly language program and machine language instructions.
Introduction
Assembly language programs can have three different types of statements such as directives, instructions, and macros are three
types of statements in Assembly Language. Since we are working with a low level programming language that does not have anything
close to self-documentation like a high level language, it is strongly advised to comment the assembly language source code adequately!
The following are skeleton_source_code_files for masm_source_32bit, masm_source_64bit, nasm_source_32bit, and nasm_source_64bit.
- directives::= a statement that tells the assembler to take some form of action, masm will require masm_directives for 32 bit code, as
far as 64 bit code with masm, most directives are not allowed. Using nasm, most directives are not needed in either 32 or 64 bit code.
- instructions::= a machine language instruction that tells the CPU to perform some type of action. There are several types
of instructions: data_movement, arithmetic, bit_manipulation, and branching.
- data_movement::= mov, push, pop
- mov::= mov_reg_reg, mov_mem_reg, mov_reg_mem, mov_mem_imm, mov_reg_imm, mov_mem_acc, mov_acc_mem, mov_seg_reg, mov_seg_mem, mov_reg_seg, mov_mem_seg
For examples of using data movement instructions (masm32, masm64, nasm32, nasm64):
masm32_mov.asm
masm64_mov.asm
nasm32_mov.asm
nasm64_mov.asm
For examples of using push and pop instructions (masm32, masm64, nasm32, nasm64):
masm32_push_pop.asm
masm64_push_pop.asm
nasm32_push_pop.asm
nasm64_push_pop.asm
- mov_reg_reg::= moves data from one register to another register.
- mov_mem_reg::= moves data from a register to a memory location value.
- mov_reg_mem::= moves data from a memory location value to a register.
- mov_mem_imm::= moves data from an immediate value to a memory location.
- mov_reg_imm::= moves data from an immediate value to a register.
- mov_mem_acc::= moves accumulator data value to a memory location.
- mov_acc_mem::= moves a memory location value to the accumulator.
- mov_seg_reg::= moves a register value into a segment register.
- mov_seg_mem::= moves a memory location value into a segment register.
- mov_reg_seg::= moves a segment register value into a non-segment register.
- mov_mem_seg::= moves a segment register value into a memory location value.
- push::= push_reg, push_mem, push_seg, push_all, push_flags
- push_reg::= pushes content of a register onto the stack.
- push_mem::= pushes content of a memory location onto the stack.
- push_seg::= pushes content of a segment register onto the stack.
- push_all::= pushes content of all registers onto the stack.
- push_flags::= pushes flags onto stack.
- pop::= pop_reg, pop_mem, pop_seg, pop_all, pop_flags
- pop_reg::= pops top element of the stack and places value in register.
- pop_mem::= pops top element of the stack and places value in memory location.
- pop_seg::= pops top element of the stack and places value in segment register.
- pop_all::= pops top elements for all registers off the stack and place in respective registers.
- pop_flags::= pops flags off the stack.
- arithmetic::= addition, subtraction, multiplication, division, increment, decrement, negation
- addition::= add destination, source - adds the content of source to destination, destination = destination + source
- subtraction::= sub destination, source - subracts the content of source from destination, destination = destination - source
- multiplication::= mul_source, imul_source, imul_destination_source, imul_destintation_source_immediate
- mul_source::= multiplies the accumulator by source, which can be a register or memory location. Accumulator is AL if source is byte,
AX if source is word, EAX if source is dword, and RAX if source is a qword, DX, EDX, RDX can contain the high order of the result if needed.
Example: mul bx <- ax = ax * bx, since this is 16 bits times 16 bits, result could be 32 bits and is stored in DX:AX, where DX is high word.
- imul_source::= signed multiplication of accumulator and source, see mul_source as they are similar.
Example: imul bx <- ax = ax * bx, since this is 16 bits times 16 bits, result could be 32 bits and is stored in DX:AX, where DX is high word.
- imul_destination_source::= signed multiplication of destination register and source; destination = destination * source
Example: imul bx, cx <- bx = bx * cx, since this is 16 bits times 16 bits, result could be 32 bits and is stored in DX:AX, where DX is high word.
- imul_destintation_source_immediate::= signed multiplication of destination register, source, and immediate value, destination = source * immediate
Example: imul bx, cx, 4 <- bx = cx * 4, since this is 16 bits times 16 bits, result could be 32 bits and is stored in DX:AX, where DX is high word.
- division::= div_source, idiv_source
- div_source::= divides implicit_operand (dividend) by source, where source is the divisor.
- implicit_operand::= if source is a byte, dividend is AX, with AL containing quotient, AH containing remainder; if source is word,
dividend is DX:AX, with AX containing quotient, DX containing remainder; if source is doubleword, dividend is EDX:EAX, with EAX containing
quotient, EDX containing remainder; if source is quadword, dividend is RDX:RAX, with RAX containing quotient, RDX containing remainder. Division
is defined by: dividend = quotient * divisor + remainder.
- idiv_source::= signed division, see div_source as they are similar.
- increment::= inc destination - increments the destination by one, destination can be a register or memory location.
- decrement::= dec destination - decrements the destination by one, destination can be a register or memory location.
- negation::= neg destination - negates the destination, destination can be a register or memory location.
- bit_manipulation::= logical_operations, shift, rotate
- logical_operations::= and, or, xor, not
- and::= and destination, source - destination = destination AND source, destination and source can be registers and/or memory locations
- or::= or destination, source - destination = destination OR source, destination and source can be registers and/or memory locations
- xor::= xor destination, source - destination = destination XOR source, destination and source can be registers and/or memory locations
- not::= not destination, not all (or one's complement) of the bits in the destination, destination can be a register or memory location
- shift::= shl, shr, sal, sar
- shl::= shl source_reg/mem, reg/imm, logically shift left source_reg/mem the number of bits specified in reg/imm. Most significant bit goes into carry flag, and
least significant bit is zeroed.
- shr::= shr source_reg/mem, reg/imm, logically shift right source_reg/mem the number of bits specified in reg/imm. Least significant bit goes into carry flag, and
most significant bit is zeroed.
- sal::= sal source_reg/mem, reg/imm, arithmetically shift left source_reg/mem the number of bits specified in reg/imm. Most significant bit goes into carry flag, and
least significant bit is zeroed.
- sar::= sar source_reg/mem, reg/imm, arithmetically shift right source_reg/mem the number of bits specified in reg/imm. Least significant bit goes into carry flag, and
most significant bit remains unchanged!
- rotate::= rol, ror, rcl, rcr
- rol::= rol source_reg/mem, reg/imm, rotate left source_reg/mem the number of bits specified in reg/imm. Most significant bit goes into carry flag AND moved into the
least significant bit.
- ror::= ror source_reg/mem, reg/imm, rotate right source_reg/mem the number of bits specified in reg/imm. Least significant bit goes into carry flag AND moved into the
most significant bit.
- rcl::= rcl source_reg/mem, reg/imm, rotate carry left source_reg/mem the number of bits specified in reg/imm. Most significant bit goes into carry flag AND old carry
flag value is moved into the least significant bit.
- rcr::= rcr source_reg/mem, reg/imm, rotate carry right source_reg/mem the number of bits specified in reg/imm. Least significant bit goes into carry flag AND old carry
flag value is moved into the most significant bit.
- branching::= jmp and j_cc (jump on condition code) - More at:
asm.control.flow.html
- macros::= a shorthand sequence of directives, instructions, and even other macros used as a statement.
- comments::= most assemblers use the ; character to designate a comment.
- skeleton_source_code_files::= a skeleton source code files are bare bones minimum files for creating a program.
- masm_directives::=
- masm_source_32bit::= the following is a skeleton source code file for 32 bit programs written for masm:
masm_skel_32.asm
- masm_source_64bit::= the following is a skeleton source code file for 64 bit programs written for masm:
masm_skel_64.asm
- nasm_source_32bit::= the following is a skeleton source code file for 32 bit programs written for nasm:
nasm_skel_32.asm
- nasm_source_64bit::= the following is a skeleton source code file for 64 bit programs written for nasm:
nasm_skel_64.asm
- masm::= Microsoft ASseMbler, found incorporated with Visual Studio.
- nasm::= Netwide ASseMbler, used for both Windows and Linux operating systems.
- byte::= 8 bits
- word::= 16 bits, two byte
- doubleword::= 32 bits, four byte, two word, same as dword
- dword::= 32 bits, four byte, two word ( doubleword )
- quadword::= 64 bits, eight byte, four word, two dword, same as qword
- qword::= 64 bits, eight byte, four word, two dword ( quadword )
. . . . . . . . . ( end of section Assembly Language Basics) <<Contents | Index>>