ALU Instructions

operationR-formatI-format
addaddaddi
subtractsubsubi
ANDandandiandhi
ORororiorhi
XORxorxorixorhi
NORnornori

pseudo-instruction

The add/subtract instructions assume a sign-extended immediate field. The logic instructions do not use sign-extenstion.

The sign extension operation is described below.

Register Transfer (move)

Move register to register (using add)
    mov rc, ra # rC <= rA + zero

Move signed immediate to register (using addi)
    movi rb, immed # rB <= zero + se(immed)

Move immediate to upper 16-bits (high half) register (using orhi)
    movhi rb, immed # rB[31:16] <= zero | immed

Moving a general 32-bit constant requires two instructions.

For example r2 = 0x2AB_CD87 becomes:


    movhi   r2,684       ; 684 is 0x2AC
    addi    r2,r2,-12921 ; -12921 is 0xFFFF_CD87

The three mov instructions are implemented by pseudo-instructions.

Verilog for sign-extension


zero = 16'h0000;
ones = 16'hffff;
immedse = { (immed[15]?ones:zero), immed};

C Source

Download: cprog1.zip.


01: // Arithmetic (addition/subtraction) and
02: // Logic Operations
03: 
04: int main()
05: { 
06:         int na, nb, nc, nd, ne;
07:  
08:         na = 14;
09:         nb = na ^ 0x2ABCD87;
10:         nc = 0x1234ABCD;
11:         nd = na ^ nc;
12:         nb = na | nd;
13:         ne = (na | 0x3A)&~(nb^0xF);
14:         nc = (na + nb) - nc + 21;
15:         nd = ~na;
16:         /* Event loop never exits. */
17:         while (1);
18: }

Disassembly

0000806c <main>:
// Arithmetic (addition/subtraction) and
// Logic Operations

int main()
{ 
    806c:    defffa04     addi    sp,sp,-24
    8070:    df000515     stw    fp,20(sp)
    8074:    d839883a     mov    fp,sp
    int na, nb, nc, nd, ne;
 
    na = 14;
    8078:    00800384     movi    r2,14
    807c:    e0800015     stw    r2,0(fp)
    nb = na ^ 0x2ABCD87;
    8080:    e0c00017     ldw    r3,0(fp)
    8084:    0080ab34     movhi    r2,684
    8088:    10b361c4     addi    r2,r2,-12921
    808c:    1884f03a     xor    r2,r3,r2
    8090:    e0800115     stw    r2,4(fp)
    nc = 0x1234ABCD;
    8094:    00848d74     movhi    r2,4661
    8098:    10aaf344     addi    r2,r2,-21555
    809c:    e0800215     stw    r2,8(fp)
    nd = na ^ nc;
    80a0:    e0c00017     ldw    r3,0(fp)
    80a4:    e0800217     ldw    r2,8(fp)
    80a8:    1884f03a     xor    r2,r3,r2
    80ac:    e0800315     stw    r2,12(fp)
    nb = na | nd;
    80b0:    e0c00017     ldw    r3,0(fp)
    80b4:    e0800317     ldw    r2,12(fp)
    80b8:    1884b03a     or    r2,r3,r2
    80bc:    e0800115     stw    r2,4(fp)
    ne = (na | 0x3A)&~(nb^0xF);
    80c0:    e0800017     ldw    r2,0(fp)
    80c4:    10c00e94     ori    r3,r2,58
    80c8:    e0800117     ldw    r2,4(fp)
    80cc:    108003dc     xori    r2,r2,15
    80d0:    0084303a     nor    r2,zero,r2
    80d4:    1884703a     and    r2,r3,r2
    80d8:    e0800415     stw    r2,16(fp)
    nc = (na + nb) - nc + 21;
    80dc:    e0c00017     ldw    r3,0(fp)
    80e0:    e0800117     ldw    r2,4(fp)
    80e4:    1887883a     add    r3,r3,r2
    80e8:    e0800217     ldw    r2,8(fp)
    80ec:    1885c83a     sub    r2,r3,r2
    80f0:    10800544     addi    r2,r2,21
    80f4:    e0800215     stw    r2,8(fp)
    nd = ~na;
    80f8:    e0800017     ldw    r2,0(fp)
    80fc:    0084303a     nor    r2,zero,r2
    8100:    e0800315     stw    r2,12(fp)
    /* Event loop never exits. */
    while (1);
    8104:    003fff06     br    8104 <main+0x98>


Maintained by John Loomis, last updated 28 July 2008