Search code examples
assembly8051

How to calculate size of an instruction (ADD A,Rn --> 1byte..?) in 8051 assembly programming?


I'm new to Assembly programming,

ADD A,Rn

was 1-byte instruction and

ADD A,direct

was 2-byte instruction. Why?


Solution

  • ADD A,Rn can encode the opcode + register number in one sole byte, hence the 1-byte instruction.

    There are 8 different opcodes that hold on 1 byte which is composed of:

    base_operand = 0x28 (only 6 bits are required)
    R_register_index = 0->7 (only 3 bits are required)
    
    instruction = base operand OR R_register_index
    
    ADD A,R0    0x28
    ADD A,R1    0x29
    ADD A,R2    0x2A
    ADD A,R3    0x2B
    ADD A,R4    0x2C
    ADD A,R5    0x2D
    ADD A,R6    0x2E
    ADD A,R7    0x2F
    

    ADD A,direct needs 1 byte for the opcode and at least another byte to hold the parameter, which explains the size > 1 (ex: MOV RO,#12H: 2 bytes)

    Instruction size ranges from 1 to 3 bytes, but there's no absolute rule about the size of the instructions. (for some instructions with 2 parameters, read from memory)

    Better check the reference here where you get all the instructions & all their sizes in memory.

    The more reliable way to compute the size is to build an instruction size table (IST): opcode => size.

    That's how the debuggers do to "predict" the next instruction address.

    For the sake of completeness, let me provide you this table (note that $A5 is reserved and is not an instruction: I set size to 0)

    op     sz
    $00 => 1
    $01 => 2
    $02 => 3
    $03 => 1
    $04 => 1
    $05 => 2
    $06 => 1
    $07 => 1
    $08 => 1
    $09 => 1
    $0A => 1
    $0B => 1
    $0C => 1
    $0D => 1
    $0E => 1
    $0F => 1
    $10 => 3
    $11 => 2
    $12 => 3
    $13 => 1
    $14 => 1
    $15 => 2
    $16 => 1
    $17 => 1
    $18 => 1
    $19 => 1
    $1A => 1
    $1B => 1
    $1C => 1
    $1D => 1
    $1E => 1
    $1F => 1
    $20 => 3
    $21 => 2
    $22 => 1
    $23 => 1
    $24 => 2
    $25 => 2
    $26 => 1
    $27 => 1
    $28 => 1
    $29 => 1
    $2A => 1
    $2B => 1
    $2C => 1
    $2D => 1
    $2E => 1
    $2F => 1
    $30 => 3
    $31 => 2
    $32 => 1
    $33 => 1
    $34 => 2
    $35 => 2
    $36 => 1
    $37 => 1
    $38 => 1
    $39 => 1
    $3A => 1
    $3B => 1
    $3C => 1
    $3D => 1
    $3E => 1
    $3F => 1
    $40 => 2
    $41 => 2
    $42 => 2
    $43 => 3
    $44 => 2
    $45 => 2
    $46 => 1
    $47 => 1
    $48 => 1
    $49 => 1
    $4A => 1
    $4B => 1
    $4C => 1
    $4D => 1
    $4E => 1
    $4F => 1
    $50 => 2
    $51 => 2
    $52 => 2
    $53 => 3
    $54 => 2
    $55 => 2
    $56 => 1
    $57 => 1
    $58 => 1
    $59 => 1
    $5A => 1
    $5B => 1
    $5C => 1
    $5D => 1
    $5E => 1
    $5F => 1
    $60 => 2
    $61 => 2
    $62 => 2
    $63 => 3
    $64 => 2
    $65 => 2
    $66 => 1
    $67 => 1
    $68 => 1
    $69 => 1
    $6A => 1
    $6B => 1
    $6C => 1
    $6D => 1
    $6E => 1
    $6F => 1
    $70 => 2
    $71 => 2
    $72 => 2
    $73 => 1
    $74 => 2
    $75 => 3
    $76 => 2
    $77 => 2
    $78 => 2
    $79 => 2
    $7A => 2
    $7B => 2
    $7C => 2
    $7D => 2
    $7E => 2
    $7F => 2
    $80 => 2
    $81 => 2
    $82 => 2
    $83 => 1
    $84 => 1
    $85 => 3
    $86 => 2
    $87 => 2
    $88 => 2
    $89 => 2
    $8A => 2
    $8B => 2
    $8C => 2
    $8D => 2
    $8E => 2
    $8F => 2
    $90 => 3
    $91 => 2
    $92 => 2
    $93 => 1
    $94 => 2
    $95 => 2
    $96 => 1
    $97 => 1
    $98 => 1
    $99 => 1
    $9A => 1
    $9B => 1
    $9C => 1
    $9D => 1
    $9E => 1
    $9F => 1
    $A0 => 2
    $A1 => 2
    $A2 => 2
    $A3 => 1
    $A4 => 1
    $A5 => 0
    $A6 => 2
    $A7 => 2
    $A8 => 2
    $A9 => 2
    $AA => 2
    $AB => 2
    $AC => 2
    $AD => 2
    $AE => 2
    $AF => 2
    $B0 => 2
    $B1 => 2
    $B2 => 2
    $B3 => 1
    $B4 => 3
    $B5 => 3
    $B6 => 3
    $B7 => 3
    $B8 => 3
    $B9 => 3
    $BA => 3
    $BB => 3
    $BC => 3
    $BD => 3
    $BE => 3
    $BF => 3
    $C0 => 2
    $C1 => 2
    $C2 => 2
    $C3 => 1
    $C4 => 1
    $C5 => 2
    $C6 => 1
    $C7 => 1
    $C8 => 1
    $C9 => 1
    $CA => 1
    $CB => 1
    $CC => 1
    $CD => 1
    $CE => 1
    $CF => 1
    $D0 => 2
    $D1 => 2
    $D2 => 2
    $D3 => 1
    $D4 => 1
    $D5 => 3
    $D6 => 1
    $D7 => 1
    $D8 => 2
    $D9 => 2
    $DA => 2
    $DB => 2
    $DC => 2
    $DD => 2
    $DE => 2
    $DF => 2
    $E0 => 1
    $E1 => 2
    $E2 => 1
    $E3 => 1
    $E4 => 1
    $E5 => 2
    $E6 => 1
    $E7 => 1
    $E8 => 1
    $E9 => 1
    $EA => 1
    $EB => 1
    $EC => 1
    $ED => 1
    $EE => 1
    $EF => 1
    $F0 => 1
    $F1 => 2
    $F2 => 1
    $F3 => 1
    $F4 => 1
    $F5 => 2
    $F6 => 1
    $F7 => 1
    $F8 => 1
    $F9 => 1
    $FA => 1
    $FB => 1
    $FC => 1
    $FD => 1
    $FE => 1
    $FF => 1