3 - Assembly Language Basics Flashcards

1
Q

What does “main” do?

A

It starts the main procedure, the entry point for the program.

You start with “main PROC” and end with “main ENDP”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does ExitProcess do?

A

It stops the program and returns control to the operating system.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you declare a variable called “sum”?

A

Call the data segment and declare a “sum” doubleword by saying

.data
sum DWORD 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How would you add the sum of 5 and 6 and store it into sum?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How are integer literals declared?

A

[ {+ | - } ] 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How are constant integer expressions declared?

A

A mathematical expression involving integer literals and arithmetic operations that can only be evaluated at assembly time.

Operator precedence

  1. Parentheses
  2. Unary plus, minus
  3. Multiply, divide
  4. Modulus
  5. Add, subtract
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How are real number literals declared?

A

[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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How are character literals declared?

A

A single character enclosed in single or double quotes. These are stored internally as integers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How are string literals declared?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are reserved words?

A

Reserved words are not case-sensitive. (e.g. MOV is the same as mov)

  1. Instruction mnemonics, like MOV, ADD, and MUL
  2. Register names
  3. Directives
  4. Attributes (BYTE, WORD)
  5. Operators, used in constant expressions
  6. Predefined symbols (like @data), which return constant integer values at assembly time.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are identifiers?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are directives?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How are segments defined?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are instructions?

A

[label:] mnemonic [operands] [;comment]

An instruction is a statement that becomes executable when a program is assembled.

The instruction contains four basic parts:

  1. label (optional)
  2. instruction mnemonic (required)
  3. Operand (usually required)
  4. Comment (optional)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a label?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are data labels?

A

A data label identifies the location of a variable. For example, this defines a variable named count:
“count DWORD 100”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are code labels?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is an instruction mnemonic?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is an operand?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How many operands do each instruction have?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do you create comments?

A

Single line comments with the semicolon.

Writing COMMENT and putting text between symbols (!, &, etc)

COMMENT !
This line is a comment
!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the NOP?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are the necessary declarations?

A

.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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Why would your operating system need to know if the program completed successfully?

A

System administrators often create script files that execute programs in sequence. If it didn’t, then they can’t go onto the next program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is the .MODEL directive?

A

.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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is the .STACK directive?

A

.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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the .CODE directive?

A

.code
main PROC

It’s the beginning of the code area of the program (meaning what’s afterwards is usually the main procedure).

28
Q

What are the ENDP directive and END directive?

A

The ENDP is the end of a procedure. Therefore, to end the main procedure, you say “main ENDP”.

The END directive marks the very end of the program, and references the program entry point: “END main”.

29
Q

How does a source program written in assembly language become executable code?

A

The Assemble-Link-Execute cycle.

First, a programmer can use a text editor to create the source file. Then, the assembler reads the source file and produces an object file (machine language translation) or listing file.

The linker reads the object file and checks for any calls to procedures in a link library. The linker copies any required procedures and combines them with the object file, and produces the executable file.

The operating system loader utility reads the executable file into memory and branches the CPU to the program’s starting address, and the program begins to execute.

30
Q

What are the intrinsic data types?

A

The assembler recognizes a basic set of intrinsic data types, which describe types in terms of their size (byte, doubleword, and so on), whether they are signed, and whether they are integers or reals.

BYTE - 8-bit unsigned integer
SBYTE - 8 bit signed integer
WORD - 16-bit unsigned integer
SWORD - 16-bit signed integer
DWORD - 32-bit unsigned integer
SDWORD - 32-bit signed integer
FWORD - 48-bit integer
QWORD - 64-bit integer
TBYTE - 80-bit integer
REAL4 - 32-bit IEEE short real
REAL8 - 64-bit IEEE long real
REAL10 - 80-bit IEEE extended real
31
Q

What is a data definition statement?

A

[name] directive initializer [ , initializer]

The directive can be either intrinsic data types or one of the following legacy data directives:
DB - 8-bit integer
DW - 16-bit integer
DD 32-bit integer or real
DQ 64-bit integer or real
DT - define 80-bit integer

There MUST be at least one initializer, even zero. Additional initializers are separated by commas. If you want to leave it uninitialized, you can assign it ? symbol for a random value. All initializers are converted to binary data by the assembler.

32
Q

How do you define 8-bit data?

A

BYTE, SBYTE, or DB. Each initializer MUST fit into 8 bits of storage. For example:

value1 BYTE 'A'
value2 BYTE 0
value3 BYTE 255
value4 SBYTE -128
value5 SBYTE +127
value6 BYTE ?
33
Q

What does it mean if there are multiple initializers?

A

Then, its label refers only to the offset of the first initializer. For example, if list is at 0000, then 10 is at 0000, 20 is at offset 0001, 20 is at 0002, and 40 is at 0003.

list BYTE 10, 20, 30, 40

Additionally, you can continue an array without labels.

list BYTE 10,20,30,40
BYTE 50,60,70,80

You can ALSO use different radixes. For example:

list1 BYTE 10, 32, 41h, 00100010b
list2 BYTE 0Ah, 20h, ‘A’, 22h

34
Q

How do you define strings?

A

You just need to enclose them in single or double quotes PLUS the null-terminating 0.

greeting1 BYTE “Good night”, 0

A string can be divided between multiple lines without having to supply a label for each line

greeting 1 BYTE “Welcome to ..”
BYTE “in”, 0dh, 0ah

35
Q

What are the end-of-line characters and the line continuation character?

A

The end-of-line characters include 0Dh and 0Ah, called CR/LF (carriage return line feed).

The line continuation character concatenates two source code lines into a single statement, and it MUST be the last character on the line:

greeting1 /
BYTE “Welcome to program “

36
Q

What is the DUP operator?

A

It allocates storage for multiple data items, using an integer expression as a counter. It’s useful when allocating space for a string or array.

BYTE 20 DUP (0) ;20 bytes, all 0
BYTE 20 DUP (?) ;20 bytes, unitialized
BYTE 4 DUP (“HI”) ;20 bytes “HIHIHI..”

37
Q

How do you declare 16-bit data?

A

You can use WORD, SWORD, or DW. For example, you can say

word1 WORD 65535
word2 SWORD -32768
word3 WORD ?

38
Q

How do you create an array of 16-bit data?

A

Either listing the elements or using the DUP operator.

myList WORD 1, 2, 3, 4, 5
array 5 WORD DUP(?)

39
Q

How do you allocate 32-bit data?

A

The DWORD directive and SDWORD directive, as well as DD.

val1 DWORD 12345678h
val2 SDWORD -214783648
val3 DD -2147483648

40
Q

How do you allocate 64-bit data?

A

The QWORD directive and DQ directive.

quad1 QWORD 1234567812345678h

41
Q

How do you define packed binary coded decimal data?

A

In TBYTEs, or a 10-byte package. Each byte except the highest contains two decimal digits.. The highest contains the numbers sign (80h for negative, 00h for positive).

RULE: Constant initializers MUST be hexadecimal

e.g. intVal TBYTE 1234h
NOT e.g. intVal TBYTE -1234

42
Q

How can you encode a real number as packed BCD?

A

First, you can load it onto floating-point register stack with the fld instruction and then use the FBSTP instruction to convert it to packed BCD.

.data
posVal REAL8 1.5
bcdVAL TBYTE ?

.code
fld posVal
fbstp bcdVal ; rounds up to 2

43
Q

How are floating-point types defined?

A

REAL4 defines a 4-byte single-precision floating point variable. REAL8 defines an 8-byte double precision value. Also, DD, DQ, and DT all define real numbers.

rVal1 REAL4 -1.2
rVal2 REAL8 3.2 E-260
shortArray REAL4 20 DUP(0.0)

44
Q

How do you write a program that adds three variables?

A

.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD

.data
firstval DWORD 20002000h
secondval DWORD 11111111h
thirdval DWORD 22222222h
sum DWORD 0
.code
main PROC
   mov eax, firstval
   add eax, secondval
   add eax, thirdval
   mov sum, eax
   INVOKE ExitProcess,0
main ENDP
END main
45
Q

What is little-endian order?

A

x86 processors use this order (low to high). The least significant byte is stored at the first memory address.

e.g. 12345678h
0000 - 78
0001 - 56
0002 - 34
0003 - 12
46
Q

What is big-endian order?

A

Other processors order things from high to low end. The most significant byte is stored at the first memory address.

e.g. 12345678h
0000 - 12
0001 - 34
0002 - 56
0003 - 78
47
Q

What directive can you use to declare uninitialized data?

A

The .DATA? (with a question mark) directive. This will reduce the size of a program.

For example, the second will be 20,000 bytes larger than the first.

.data?
bigArray DWORD 5000 DUP(?)

.data
bigArray DWORD 500 DUP(?)

48
Q

Can you switch between code and data?

A

Yes. In fact, you may want to do that when using a variable only within a localized area of a program.

e.g.
.code 
mov eax, ebx
.data
temp DWORD ?
.code
mov temp, eax
49
Q

How are symbolic constants created?

A

By associating an identifier (a symbol) with an integer expression or some text. Symbols do not reserve storage, and they are only used by the assembler when scanning a program, and they cannot change at runtime.

You can use the equal sign directive to create symbols, using EQU and TEXTEQU.

50
Q

How do you use the equal-sign directive?

A

For example, if you said
COUNT = 500
mov eax, COUNT

then MASM will scan the source file and produce the corresponding lines.

51
Q

What is the current location counter?

A

The dollar sign, $, is called the current location counter. For example, this will initialize selfPtr with its own offset value:

selfPtr DWORD $

52
Q

How do you do keyboard definitions?

A

Esc_key = 27
mov a1, Esc_key

A statement is more self-describing if it uses the symbol rather than an integer literal.

53
Q

How do you do redefinitions?

A

Just use = within the same program, as you would with C++. For example, this would change the value of al thrice:

COUNT = 5
mov al, COUNT
COUNT = 10
mov al, COUNT
COUNT = 100
mov al, COUNT
54
Q

How do you calculate the size of byte arrays and strings?

A

list BYTE 10, 20, 30, 40
ListSize = 4

While you can declare the size of the list, it’s better to let the assembler calculate its value for you. The $ operator (current location counter) returns the offset associated with the current program statement. ListSize MUST immediately follow list.

list BYTE 10, 20, 30, 40
ListSize = ($ - list)

55
Q

How do you calculate the size of arrays of words and doublewords?

A

When calculating the number of elements, you divide the total array size (in bytes) by the size of the individual elements (in bytes). For example, words are 2 bytes and doublewords are 4 bytes

list WORD 1000h, 2000h, 3000h, 4000h
ListSize = ($ - list)/2

list DWORD 10000000h, 20000000h, 30000000h, 40000000h
ListSize = ($ - list) / 4

56
Q

What is the EQU directive?

A

It associates a symbolic name with an integer expression or some arbitrary text. However, it CANNOT be redefined. There are three formats:

name EQU expression
name EQU symbol
name EQU carrot text carrot

For example, you can define pi:
PI EQU carrot 3.14159 carrot

Or you can create a menu string:
pressKey EQU *carrot* "Press any key to continue.., 0" *carrot*
...
.data
prompt BYTE pressKey
57
Q

What is the TEXTEQU directive?

A

For text substitution. It creates a text macro that CAN be redefined at any time. There are three formats: one assigns text, one assigns the contents of an existing text macro, and the third assigns a constant integer expression.

name TEXTEQU carrot text carrot
name TEXTEQU textmacro
name TEXTEQU %constExpr

For example, the prompt1 variable uses the continueMsg text macro:

continueMsg TEXTEQU carrot “Continue? (Y/N)” carrot
.data
prompt1 BYTE continueMsg

58
Q

How can text macros build on each other?

A

You can set count equal to the value of an integer expression with rowSize. Then the symbol move is defined as mov. Finally, setupAL is built from move and count:

rowSize = 5
count TEXTEQU %(rowSize*2)
move TEXTEQU crt mov crt
setupAL TEXTEQU crt move al, count crt

The statement setupAL would be assembled as mov al, 10.

59
Q

Declare a symbolic constant using the equal-sign directive that contains the ASCII code (08h) for the Backspace key.

A

BACKSPACE = 08h

60
Q

Declare a symbolic constant named SecondsInDay using the equal-sign directive and assign it an arithmetic expression that calculates the number of seconds in a 24-hour period.

A

SecondsInDay = 60 * 60 * 24

61
Q

Write a statement that causes the assembler to calculate the number of bytes in the following array, and assign the value to a symbolic constant named ArraySize:

myArray WORD 20 DUP (?)

A

myArray WORD 20 DUP (?)
ArraySize = ($ - myArray)

*** NOT divided by two because NOT the number of elements, the number of bytes

62
Q

Show how to calculate the number of elements in the follownig array, and assign the value to a symbolic constant named ArraySize.

A

myArray DWORD 30 DUP(?)

ArraySize = ($ - myArray) /4

63
Q

Use a TEXTEQU expression to redefine “proc” as “procedure.”

A

PROCEDURE TEXTEQU crt PROC crt

64
Q

Use TEXTEQU to create a symbol named Sample for a string constant, and then use the symbol when defining a string variable named MyString.

A

Sample TEXTEQU crt “Hello” crt
MyString BYTE Sample

*** Remember that strings have to be in for TEXTEQU and that you use BYTE to set strings equal to each other. The equal sign directive is ONLY for symbolic constants because it CANNOT assign storage.

65
Q

Use TEXTEQU to assign the symbol SetupESI to the following line of code:

mov esi, OFFSET myArray

A

SetupESI TEXTEQU crt mov esi, OFFSET myArray crt