asked 135k views
5 votes
1. Convert C to assembly. Assume all variables are integers, and initialized before these code blocks. Assume that there is code before and after these blocks, and that each part is independent from each other. a. Assume a is stored in RO, b is stored in R1, and is stored in R2. a = 1; b = 6; c = a + b; a b = a - c; c++; a <<= 3; b. Assume a is stored in RO, b is stored in R1, and c is stored in R2. if (a < b) a + C; } else! a - C; C. } Assume a is stored in RO, b is stored in R1, and c is stored in R2, x is stored in R3. for (x 10; x > 0; --) { C = C + x; a = a + c; b = a + x; } d. Assume a is stored in RO, b is stored in R1, and c is stored in R2. if ((a < 2 && a >= b) 11 (b != c)) { a += 2; } else! а = b;

1 Answer

0 votes

Step-by-step explanation:

Here's the conversion of the given C code blocks into assembly code, assuming the variables are stored in the specified registers:

a.

```assembly

li $r0, 1 # a = 1

li $r1, 6 # b = 6

add $r2, $r0, $r1 # c = a + b

sub $r0, $r0, $r2 # a = a - c

addi $r2, $r2, 1 # c++

sll $r0, $r0, 3 # a <<= 3

```

b.

```assembly

slt $r3, $r0, $r1 # if (a < b)

beqz $r3, else_label # jump to else_label if a >= b

add $r0, $r0, $r2 # a + c

j end_label # jump to end_label

else_label:

sub $r0, $r0, $r2 # a - c

end_label:

```

c.

```assembly

li $r3, 10 # x = 10

loop:

bgtz $r3, loop_end # exit loop if x <= 0

add $r2, $r2, $r3 # C = C + x

add $r0, $r0, $r2 # a = a + C

add $r1, $r0, $r3 # b = a + x

subi $r3, $r3, 1 # x--

j loop # jump to loop

loop_end:

```

d.

```assembly

slt $r3, $r0, 2 # if (a < 2)

beqz $r3, else_label # jump to else_label if a >= 2

slt $r3, $r0, $r1 # if (a >= b)

bnez $r3, else_label # jump to else_label if a < b

sub $r0, $r0, $r2 # a = b

j end_label # jump to end_label

else_label:

addi $r0, $r0, 2 # a += 2

end_label:

```

In the above assembly code, the instructions assume that the register `$r0` corresponds to variable `a`, `$r1` corresponds to variable `b`, and `$r2` corresponds to variable `c`. The register `$r3` is used for temporary operations or loop variables.

answered
User Karoly Nyisztor
by
8.0k points

No related questions found