Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

18
Assembly Microprocessors session 3 ing. Ernst E. Mak MSc

Transcript of Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Page 1: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Assembly Microprocessorssession 3

ing. Ernst E. Mak MSc

Page 2: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Generic Instructions Taxonomy

• input/output data• load a value (register) into a register• arithmetic• jumping / subroutine• control• bitwise operators : Boolean, Register shifts • interrupt handling• stacking

Page 3: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Load a Value into a Register

• LOAD, Mnemonic is ld• syntax: ld destination , source

• destination:

– register– memory– memory by index

• source– value– register– memory– memory by index

As a result of a LOAD, the destination is overwritten by the source.

Page 4: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Examples

• ld a,12

• ld a,12h

• ld a,b

• ld b,a

• ld a,(0100h)

• ld a,(HL)

• ld bc,01FEh

• ld (100),a

• ld (HL),a

• ld (HL),b

• ld (HL),01h

Page 5: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Addition • ADDITION, Mnemonic is add• syntax: add destination , source

• destination:

– SPECIAL register

• source– value– register– memory by index

as a result of an ADDition, the destination is overwritten by the sum of source and destination, source remains untouched

Page 6: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

addition examples

• add a,5

• add a,b

• add a,(HL)

• add HL,BC

• add HL,HL

Page 7: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Carry

• decimal addition: 861+152

8 6 11 5 2

+

1 0 1 3

1 1

Page 8: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Flags

Page 9: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Flags (2)

S CH P/V NZ

CARRY-FLAGNEGATIVE

PARITY or OVERFLOW

BCD half carry flag

ZERO flagSIGN FLAG

Page 10: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Carry Flag

• It detects a carry from bit 7 into (the non-existent) bit 8, or in the case of register-pairs, from bit 15 into what would have been bit 16. It is also used by shift and rotate instructions, in which one bit is “lost” from a register and moves into the carry. This is probably the most frequently accessed flag of all.

Page 11: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

addition with carry• addition include the carry (bit);• Mnemonic: adc, syntax similar to add

• Purpose: to calculate the effect of the carry into the next digit

Effect:

after for example: adc b the a register will be:

a:=a+b+1 if the carry was set

a:=a+b if the carry was reset

Example: HL:=254+515 (= 254+(2*256+3))

ld b,2ld c,3ld a,254add a,cld l,ald a,0adc a,bld h,a

Page 12: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Zero Flag:

• It checks whether or not the last result was actually zero. If so the flag is set to 1, but otherwise it is reset. Watch out for this flag though – it can be very deceiving – many of the register-pair instructions simply do not change it as you’d expect: instructions like DEC or ADD for instance will only change the zero flag if applied to single registers.

Page 13: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Sign Flag:

• It stores the sign (positive or negative) of the last result. A positive number resets the flag to zero, and a negative number sets it to 1. For the purposes of this flag, zero is counted as positive. The value of the S flag is therefore always equal to the leftmost bit of the result. It may be tested using instructions like JP P (Jump if positive) or JP M (Jump if negative (Minus)).

Page 14: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Parity/Overflow Flag:

• It does two jobs at one.

1. The Parity of a result is either odd or even, depending on the number of 1’s in the result (in binary).

• If the parity is even, the flag is 1 (an odd number), • if the parity is odd, the flag is 0 (an even number).• Generally, the parity is involved at boolean

operations

2. An Overflow represents an “accidental” change of sign of result – a carry from bit 6 into bit 7 effectively.

• Generally, this flag is involved at additions, subtractions, increases and decreases

Page 15: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Other Flags

• Subtract Flag: It is also called the N flag, it simply lets the machine know whether or not the last instruction was an addition, or a subtraction. You can’t get hold of this flag unless you make use of PUSH and POP.

• Half-Carry Flag: It sets if there is a carry from bit 3 into bit 4, or, in the case of register-pairs, from bit 11 into bit 12. It is used internally by the Z80 for such instructions as DAA, but can not be easily tested by the programmer.

Page 16: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

WARNING WITH FLAGS

Flags are changed in a rather limited fashion.

Some instructions render one or more flags untouched, so they keep reflecting the state of an earlier instruction

Page 17: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

Jumping• Jump, Mnemonic is jp• syntax: jp {condition ,} destination

• conditions (optional):

C , NC carryflag

Z , NZ zero flag

M , P minus.positive

PO,PE parity odd/even

• destination

– memory location– label

as a result of a JumP. the program commences at the destination of the jump.

Page 18: Assembly Microprocessors session 3 ing. Ernst E. Mak MSc.

COMPARE• Compare, Mnemonic is cp

• syntax: cp reference

• reference– value

– register

– memory pointed by index

• The result of a ComPare is, that flags are set as if we

would have substracted the referenced value from the A

register. but the substraction is never performed