4 - Data Transfers, Add/Subract, JMP/LOOPS Flashcards
How can instructions have zero, one, two, or three operands?
mnemonic
mnemonic [destination]
mnemonic [destination], [source]
mnemonic [destination], [source1], [source2]
What are the 3 basic types of operands?
Immediate - uses a numeric literal expression
Register - uses a named register in the CPU
Memory - references a memory location
What are the simple notations for the standard operand types?
reg8 - 8-bit general purpose register reg16 - 16-bit general-purpose register reg32 - 32-bit general purpose register reg - any general-purpose register sereg - 16-bit segment register
imm - 8-, 16-, or 32-bit immediate value
imm8 - 8-bit immediate byte value
imm16 - 16-bit immediate word value
imm32 - 32-bit immediate doubleword value
reg/mem8 - 8-bit operand, either general register or memory byte
reg/mem16 - 16-bit operand, either general register or memory word
reg/mem32 - 32-bit operand, either general register or memory doubleword
mem - an 8-, 16-, or 32-bit memory operand
How can you use a direct memory operand to dereference a variable?
You can just move something from memory into a register, and read the machine code. For example, if you declared a variable var1, and it was offsetted at 10400h, you could copy it into an AL (8-bit) register
.data
var1 BYTE 10h
mov a1 var1
It would be assembled into the following machine instruction. The first byte in the machine instruction is the operation code (opcode), and the rest is the 32-bit hexadecimal address of var1.
A0 00010400
What are the rules for MOV instruction?
- Both operands MUST be the same size
- Both operands CANNOT be memory operands
- The instruction ponter register (IP, EIP, or RIP) cannot be a destination operand
MOV reg, reg MOV mem, reg MOV reg, mem MOV mem, imm MOV reg, imm
How would you use MOV to move data from memory to memory?
Even though you can’t do it directly, you can just assign the value of the memory to a register, and then move that value to another memory operand.
.data var1 WORD ? var2 WORD ? .code mov ax, var1 mov var2, ax
How can you copy smaller values to larger ones using MOV?
If it’s an unsigned integer, then you can just mov 0 to that register (ecx) and mov that integer to cx.
However, if you have a signed integer, then if you mov 0 to that register, then mov FFF0h (-16), you’ll get the wrong number (0000FFF0h). Therefore, you need to move 0FFFFFFFFh to the register, then mov FFF0h.
What can you do with the MOVZX instruction?
You can “move with zero-extend” any smaller unsigned integer to a larger memory. For example, if you moved 10001111b into AX.
mov bx, 0A698h
movzx eax, bx ; EAX = 0000A69Bh
movzx edx, b1 ; EDX = 0000009Bh
movzx cx, b1 ; CX = 009Bh
What can you do with the MOVSX instruction?
You can “move with sign-extend”, which will extend the value to 16 or 32 bits. It is ONLY used with signed integers.
mov bx, 0A69Bh
movsx eax, bx ; EAX = FFFFA69Bh
movsx edx, b1 ; EDX = FFFFFF9Bh
movsx cx, b1 ; CX = FF9Bh
What does the LAHF instruction do?
It “loads status flags into AH” by copying the low byte of the EFLAGS register into AH. The following flags are copied: Sign, Zero, Auxiliary Carry, Parity, and Carry. Using this, you can save a copy of the flags in a variable for safekeeping.
.data saveflags BYTE ? .code lahf ;load flags into AH mov saveflags, ah ; save them into a variable
What does the SAHF instruction do?
It “stores AH into status flags” by copying AH into the low byte of EFLAGS. You can use this to retrieve the value of flags saved in variables.
mov ah, saveflags ; load saved flags into ah
sahf ; copy into flags register
What does the XCHG instruction do? How can you exchange two memory operands with it?
It “exchanges data”, namely, the contents of two operands, the same as in MOV (no memory-to-memory) except that XCHG also does NOT accept ANY immediate operands.
XCHG reg, reg
XCHG reg, mem
XCHG mem, reg
If you want to exchange two memory operands, use a register as a temporary container and combine with MOV
mov ax, val1
xchg ax, val2
mov val1, ax
What are direct-offset operands and how do you use them with bytes?
You can add a displacement to the name of a variable to access memory locations that may not have explicit labels. For example, if we have an array of bytes called arrayB, then we can access each element with their “effective address”. However, there is NO built-in range checking!
arrayB BYTE 10h, 20h, 30h, 40h, 50h
mov al, arrayB ; AL = 10h
mov a1 [arrayB +1] ; AL = 20h
mov a1 [arrayB+2] ; AL = 30h
How do you use direct-offset operands with words and doublewords?
For words, you add 2 bytes to get the next element, and for doublewords, you add 4 bytes to get to the next element.
.data arrayW WORD 100h, 200h, 300h .code mov ax, arrayW ; AX = 100h mov ax, [arrayW + 2] ; AX = 200h
.data arrayD DWORD 10000h, 20000h .code mov eax, arrayD ; EAX = 10000h mov eax, [arrayD+4] ; EAX = 20000h
What do the INC and DEC instructions do?
The INC (increment) and DEC (decrement) instructions add 1 and subtract 1 from a register or memory operand.
.data myWord WORD 1000h .code inc myWord ; myWord = 1001h mov bx, myWord dec bx ; BX = 1000h
Although the Overflow, Sign, Zero, Auxiliary Carry, and Parity flags are changed, the INC and DEC instructions do NOT affect the Carry flag!!
What does the ADD instruction do?
ADD dest, source
For example, .data var1 DWORD 10000h var2 DWORD 20000h .code mov eax, var1 ; EAX = 10000h add eax, var2 ; EAX = 30000h
The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags all change according to the destination value.
What does the SUB instruction do?
SUB dest, source
For example, .data var1 DWORD 30000h var2 DWORD 10000h .code mov eax, var1 ; EAX = 30000h sub eax, var2; EAX = 20000h
The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags all change according to the destination value.
What does the NEG instruction do?
The NEG (negate) instruction reverses the sign of a register or memory operand by converting the number to its two’s complement.
NEG reg
NEG mem
The Carry, Zero, Sign, Overflow, Auxiliary Carry, and Parity flags all change according to the destination value.
How would you write what in C++ looks like this:
Rval = -Xval + (Yval - Zval);
If the following 32-bit variables are used:
Rval SDWORD ?
Xval SDWORD 26
Yval SDWORD 30
Zval SDWORD 40
First, negate a copy of Xval and store it in a register:
mov eax, Xval
neg eax ; EAX = -26
Second, copy Yval to a register and subtract Zval:
mov ebx, Yval
sub ebx, Zval
Third, add EBX to EAX and move that to Rval:
add eax, ebx
mov Rval, eax
How are the Carry, Overflow, and Zero status flags affected by addition and subtraction?
- The Carry flag indicates unsigned integer overflow. (e.g. if instruction has 8-bit destination operand but instruction generates something larger than 11111111, then Carry flag is set)
- Overflow flag indicates signed integer overflow. (e.g. if instruction has 16-bit destination operand but it generates negatives smaller than -32768 decimal, then Overflow flag is set)
- Zero flag indicates that an operation produced zero. For example, if an operand is subtracted from another of equal value, the Zero flag is set.
How are the Sign flag, Parity flag, and Auxiliary Carry flag affected by addition and subtraction?
- The Sign flag indicates an an operation produced a negative result (if the MSB is set).
- The Parity flag indicates whether or not an even number of 1 bits occurs in the least significant byte of the destination operand.
- The Auxiliary Carry flat is set when a 1-bit carries out of position 3 in the LSB of the destination operand.
How can there be a carry flag with subtraction?
A subtract operation sets the Carry flag when a larger unsigned integer is subtracted from a smaller one. For example, if we subtract 2 from 1, using 8-bit operands:
mov al, 1
sub al, 2 ; AL = FFh, CF = 1
Because it is negative, we have “carried” a 1 for the sign.
Can there be a carry flag with INC, DEC, or NEG?
The INC and DEC instructions do NOT affect the Carry flag.
However, applying the NEG instruction to a nonzero operand ALWAYS sets the Carry flag.
What is an example of an operation that sets off the auxiliary carry flag?
If you add 1 to 0Fh, then the sum 10h contains a 1 in bit position 4 that was carried out of bit position 3.
mov al, 0Fh
add al, 1 ; AC = 1
0 0 0 0 1 1 1 1
0 0 0 0 0001
——————-
0 0 0 1 0000