operand addressing –no instruction for reg-to-reg transfer –cannot do memory-memory transfers...

11
Operand Addressing No instruction for reg-to-reg transfer Cannot do memory-memory transfers with single instruction sw instruction violates (dest, source) spec of operands Imm Reg Mem Reg Reg Mem Reg Source Destination li $t1, 0x4 la $t1, A sw $t1,A($t2) lw $t1,A($t2) C Analog temp = 0x4; temp2 = &A; A[n] = temp; temp = A[n]; Addr

Upload: kelly-morris

Post on 02-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Operand Addressing –No instruction for reg-to-reg transfer –Cannot do memory-memory transfers with single instruction –sw instruction violates (dest, source)

Operand Addressing

– No instruction for reg-to-reg transfer– Cannot do memory-memory transfers with single

instruction– sw instruction violates (dest, source) spec of

operands

Imm

Reg

Mem

Reg

Reg

Mem

Reg

Source Destination

li $t1, 0x4

la $t1, A

sw $t1,A($t2)

lw $t1,A($t2)

C Analog

temp = 0x4;

temp2 = &A;

A[n] = temp;

temp = A[n];

Addr

Page 2: Operand Addressing –No instruction for reg-to-reg transfer –Cannot do memory-memory transfers with single instruction –sw instruction violates (dest, source)

Addressing Modes• Addressing Modes• Indirect (R) Mem[Reg[R]]– Register R specifies memory address lw $t1, ($t2)

• Indexed D(R) Mem[Reg[R]+D]– Register R specifies start of memory block– Constant displacement D specifies offset lw $t1, 8($t2)

Page 3: Operand Addressing –No instruction for reg-to-reg transfer –Cannot do memory-memory transfers with single instruction –sw instruction violates (dest, source)

.dataarray: .word 0x37, 0x55, 0xF

.textla $s0, arrayla $s1, array+4la $s2, 8($s0)

li $a0, 4

lw $t0, 0($s0) lw $t3, array($a0)

lw $t1, 8($s0)lb $t2, ($s0)

Example

Page 4: Operand Addressing –No instruction for reg-to-reg transfer –Cannot do memory-memory transfers with single instruction –sw instruction violates (dest, source)

Example

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

# xp in $a0, yp in $a1swap:

lw $t0,($a0) # t0=*xplw $t1,($a1) # t1=*ypsw $t1,($a0) # *xp=t1sw $t0,($a1) # *yp=t0jr $ra

Page 5: Operand Addressing –No instruction for reg-to-reg transfer –Cannot do memory-memory transfers with single instruction –sw instruction violates (dest, source)

Understanding Swapvoid swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

Register Variable Value

$a0 xp 120

$a1 yp 124

$t0

$t1

# xp in $a0, yp in $a1swap:

lw $t0, ($a0) lw $t1, ($a1) sw $t1, ($a0) sw $t0, ($a1) jr $ra

123456

0x124

0x120

Page 6: Operand Addressing –No instruction for reg-to-reg transfer –Cannot do memory-memory transfers with single instruction –sw instruction violates (dest, source)

Register Variable Value

$a0 xp 120

$a1 yp 124

$t0 456

$t1

# xp in $a0, yp in $a1swap:

lw $t0, ($a0) lw $t1, ($a1) sw $t1, ($a0) sw $t0, ($a1) jr $ra

123456

0x124

0x120

Page 7: Operand Addressing –No instruction for reg-to-reg transfer –Cannot do memory-memory transfers with single instruction –sw instruction violates (dest, source)

Register Variable Value

$a0 xp 120

$a1 yp 124

$t0 456

$t1 123

# xp in $a0, yp in $a1swap:

lw $t0, ($a0) lw $t1, ($a1) sw $t1, ($a0) sw $t0, ($a1) jr $ra

123456

0x124

0x120

Page 8: Operand Addressing –No instruction for reg-to-reg transfer –Cannot do memory-memory transfers with single instruction –sw instruction violates (dest, source)

Register Variable Value

$a0 xp 120

$a1 yp 124

$t0 456

$t1 123

# xp in $a0, yp in $a1swap:

lw $t0, ($a0) lw $t1, ($a1) sw $t1, ($a0) sw $t0, ($a1) jr $ra

123123

0x124

0x120

Page 9: Operand Addressing –No instruction for reg-to-reg transfer –Cannot do memory-memory transfers with single instruction –sw instruction violates (dest, source)

Register Variable Value

$a0 xp 120

$a1 yp 124

$t0 456

$t1 123

# xp in $a0, yp in $a1swap:

lw $t0, ($a0) lw $t1, ($a1) sw $t1, ($a0) sw $t0, ($a1) jr $ra

456123

0x124

0x120

Page 10: Operand Addressing –No instruction for reg-to-reg transfer –Cannot do memory-memory transfers with single instruction –sw instruction violates (dest, source)

Arithmetic Operations• add $t1, $t2, $t2• (addi $t1, $t2, 0x32)

• sub $t1, $t2, $t2• mul $t1, $t2, $t2• div $t1, $t2, $t2• and (andi), or (ori), xor (xori)• sll $t1, $t2, 2 (shift left logical)• sllv (sll variable)• sra (shift right arithematic)• srl

Page 11: Operand Addressing –No instruction for reg-to-reg transfer –Cannot do memory-memory transfers with single instruction –sw instruction violates (dest, source)

• Directives (Establish initial data structure)• .ascii (store string in memory with no null-termination)• .asciiz (store string in memory with null termination)• .byte b1,..,bn• .word w1,..,wn• .word w:n (store w into n successive words)• .space n

• .data data segment• .text assembly instructions

Directives