3 - Assembly Language Basics Flashcards
What does “main” do?
It starts the main procedure, the entry point for the program.
You start with “main PROC” and end with “main ENDP”.
What does ExitProcess do?
It stops the program and returns control to the operating system.
How would you declare a variable called “sum”?
Call the data segment and declare a “sum” doubleword by saying
.data
sum DWORD 0
How would you add the sum of 5 and 6 and store it into sum?
Call the code segment and move 5 into eax, add 6 to the eax, then move eax into sum.
.data
sum DWORD 0
.code main PROC mov eax, 5 add eas, 6 mov sum, eax
INVOKE ExitProcess, 0
main ENDP
How are integer literals declared?
[ {+ | - } ] digits [ radix ]
h - hexadecimal q/o - octal d - decimal b - binary r - encoded real t - decimal (alternate) y - binary (alternate)
e.g. 1101b for binary 13
- *** Note: a hexadecimal literal beginning with a letter MUST have a leading zero.
e. g. CANNOT be A5h => MUST be 0A5h.
How are constant integer expressions declared?
A mathematical expression involving integer literals and arithmetic operations that can only be evaluated at assembly time.
Operator precedence
- Parentheses
- Unary plus, minus
- Multiply, divide
- Modulus
- Add, subtract
How are real number literals declared?
[sign] integer. [integer] [exponent]
Real number literals are represented as either decimal reals or encoded reals. At least one digit and one decimal are required. (e.g 2. +2.0 26.E5. )
How are character literals declared?
A single character enclosed in single or double quotes. These are stored internally as integers.
How are string literals declared?
A single literal is a sequence of character enclosed in single or double quotes. (e.g. ‘ABC’).
These are also stored as integer byte values, so “ABCD” contains the four bytes 41h, 42h, 43h, 44h.
What are reserved words?
Reserved words are not case-sensitive. (e.g. MOV is the same as mov)
- Instruction mnemonics, like MOV, ADD, and MUL
- Register names
- Directives
- Attributes (BYTE, WORD)
- Operators, used in constant expressions
- Predefined symbols (like @data), which return constant integer values at assembly time.
What are identifiers?
An identifier is a programmer-chosen code name, and it might identify a variable, a constant, a procedure, or a code label.
Between 1-247 characters, not case sensitive, etc, first letter must be a letter, underscore , @, ?, $, digits. However, they cannot be the same as an assembler reserved word.
What are directives?
A directive a command embedded in the source code that is recognized and acted upon by the assembler.
Directives don’t execute at runtime, but they let you define variables, macros, and procedures (e.g. DWORD tells assembler to reserve space in the program).
How are segments defined?
Segments are sections of a program that have different purposes.
.data is used to define variables
.code identifies the area of a program containing executable instructions
.stack identifies the area of a program holding the runtime stack, setting its size (e.g. .stack 100h)
What are instructions?
[label:] mnemonic [operands] [;comment]
An instruction is a statement that becomes executable when a program is assembled.
The instruction contains four basic parts:
- label (optional)
- instruction mnemonic (required)
- Operand (usually required)
- Comment (optional)
What is a label?
A label is an identifier that acts as a place marker for instructions and data. A label placed just before an instruction implies the instruction’s address.
There are two types of
labels:
1. data labels
2. code labels
What are data labels?
A data label identifies the location of a variable. For example, this defines a variable named count:
“count DWORD 100”
What are code labels?
A code label MUST end with a colon (:) character. These are used as targets of jumping and looping instructions.
For example, JMP instruction transfers control to the location marked by the label named target, creating a loop.
target:
mov ax, bx
..
jmp target
What is an instruction mnemonic?
Here are just a few examples:
MOV - move one value to another ADD - add two values SUB - subtract one value from another MUL - multiply two values JMP - jump to a new location CALL - call a procedure
What is an operand?
An operand is a value used for input and output. Instructions can have between 0 and 3 operands, which are registers, memory operands, integer expressions, or input-ouput ports.
96 - integer literal
2 + 4 - integer expression
eax - register
count - memory
How many operands do each instruction have?
It depends.
The STC instruction has no operands:
stc ; set Carry flag
The INC instruction has one operand:
inc eax ; add 1 to EAX
The MOV instruction has two operands:
mov count, ebx ; move EBX to count
How do you create comments?
Single line comments with the semicolon.
Writing COMMENT and putting text between symbols (!, &, etc)
COMMENT !
This line is a comment
!
What is the NOP?
The most useless instruction that doesn’t do any work except align code (it increments the instruction pointer)
00000000 66, 8B, C3 mov ax,bx
00000003 90 nop
00000004 8B D1 mov adx,ecx
What are the necessary declarations?
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
The .384 directive identifies it as a 32-bit program. Line 2 uses the flat memory model, and Windows requires the stdcall convention to be used. Line 3 sets aside 4096 bytes of storage, and line 4 declares a prototype for the ExitProcess function. This prototype has a PROTO keyword, a comma, and a list of input parameters (here, it’s dwExitCode, which is like returning 0 to mean it was successful).
Why would your operating system need to know if the program completed successfully?
System administrators often create script files that execute programs in sequence. If it didn’t, then they can’t go onto the next program.
What is the .MODEL directive?
.model flat, stdcall
This tells the assembler which memory model to use. In 32-bit programs, we use the flat memory model, which is associated with the processor’s protected mode.
The stdcall keyword tells the assembler how to manage the runtime stack when procedures are called.
What is the .STACK directive?
.stack
The stack directive tells how many bytes of memory to reserve for the runtime stack. 4096 happens to correspond to the size of a memory page in the processor’s system for managing memory.