This instruction selects a signed half-word (16 bits where the most significant bit is a sign bit) from the source register, and puts it in the least significant bits of the destination register. The most significant bits of the destination are the copy of the selected halfword's sign bit, so the result is a 32-bit signed word. It is a kind of equivalent of the SAR opcode but ESH works with a byte granularity, not with bits, as shown on the following diagram:
This instruction is typically used when reading a signed halfword from memory to a register, because ESH performs both alignment and sign extension in a single cycle. However memory is still accessed normally through registers, one word at a time, the IE group of instructions only adjusts the word's contents.
Usually, the halfwords come from a D register (that contains data coming from memory) and the index of the first byte inside this word is provided by a A register, but any register can be used. The index can also come from immediate data (Imm4 ou Imm16), which explains why the index is always written first in the instruction.
; Load 16 bits from memory at address 321h into register R1 : MOV 321h A1 ESH A1 D1 R1
Currently, this opcode is only assembled with the iRR and RRR forms, even though other forms are possible.
This instruction handles 16-bits words but the YASEP32's pointer have byte granularity, so unaligned half-words are possible. However, not all alignments can be adjusted by this instruction: if the two LSB of the index contain 11 then the Carry is set to indicate that more code is necessary to load the next byte of the next word. The Most Significant Bits are cleared, which eases the combination of the data with other instructions such as OR:
; load 16 bits from memory at address 123h into register R1 : MOV 123h A1 ; the 2 LSB of this address are set to 1 ESH A1 D1 R1 ; which sets the carry bit to 1 ; To process the next byte from the following ; word, R2 is used as a temporary register ADD 1 A1 CARRY ; point to the next word ESB A1 D1 R2 CARRY ; extract the first byte SHL 8 R1 CARRY ; shift by one byte OR R2 R1 CARRY ; combine high and low bytes
• ESB is similar but works on signed bytes.
• EZH is similar but works on unsigned half-words (and out-of-word alignment requires less instructions).