Assembly language LC2200 write an assembly language program equivalent to the fo
ID: 3799125 • Letter: A
Question
Assembly language LC2200
write an assembly language program equivalent to the following:
a = 0;
for (int i = 0; i < d; i++)
{
if (c != d)
a += b + (c - d) + b;
else if (c == b)
a += c + c + c + c;
else
a += (b + b) - (c + c);
}
where:
• a is located in $fp + 1
• b is located in $fp + 2
• c is located in $fp + 3
• d is located in $fp + 4
Values should be read in from memory to start with and a should be written to memory when done. You can reorder
operations as long as they produce the same result as the statement above. You should be able to run this assembly file with
different data files.
The variable i can be located in a variable or just kept in a register, whichever you prefer. This code expects that d > 0.
Inside for loop:
lw $t0, $fp, 2
lw $t1, $fp, 3
lw $t2, $fp, 4
beq $t0, $t1, cbequal
nand $t2, $t2, $t2
addi $t2, $t2, 1 #convert $t2 for negative
add $t1, $t2, $t1 #c-d = t1
add $t0, $t0, $t0
add $t0, $t1, $t0
lw $t2, $fp,1
add $t2, $t0, $t2
sw $t2, $fp, 1
halt
cbequal: beq $t1, $t2, equal
add $t0, $t0, $t0
add $t1 ,$t1, $t1
nand $t1 ,$t1, $t1
addi $t1, $t1, 1
add $t0, $t0, $t1
lw $t1, $fp, 1
add $t1, $t1, $t0
sw $t1, $fp, 1
halt
equal:add $t0, $t1, $t1
add $t0, $t1, $t0
add $t0, $t1, $t0
lw $t2, $fp, 1
add $t0, $t0, $t2
sw $t0, $fp, 1
halt
And tomake for loop work I have to use jalr, but I have no clue how to make for loop work...
Thank you in advance.
Explanation / Answer
main:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], 0
mov DWORD PTR [rbp-8], 0
.L6:
mov eax, DWORD PTR [rbp-8]
cmp eax, DWORD PTR [rbp-12]
jge .L2
mov eax, DWORD PTR [rbp-16]
cmp eax, DWORD PTR [rbp-12]
je .L3
mov eax, DWORD PTR [rbp-16]
sub eax, DWORD PTR [rbp-12]
mov edx, eax
mov eax, DWORD PTR [rbp-20]
add edx, eax
mov eax, DWORD PTR [rbp-20]
add eax, edx
add DWORD PTR [rbp-4], eax
jmp .L4
.L3:
mov eax, DWORD PTR [rbp-16]
cmp eax, DWORD PTR [rbp-20]
jne .L5
mov eax, DWORD PTR [rbp-16]
lea edx, [rax+rax]
mov eax, DWORD PTR [rbp-16]
add edx, eax
mov eax, DWORD PTR [rbp-16]
add eax, edx
add DWORD PTR [rbp-4], eax
jmp .L4
.L5:
mov eax, DWORD PTR [rbp-20]
lea edx, [rax+rax]
mov eax, DWORD PTR [rbp-16]
add eax, eax
sub edx, eax
mov eax, edx
add DWORD PTR [rbp-4], eax
.L4:
add DWORD PTR [rbp-8], 1
jmp .L6
.L2:
mov eax, 0
pop rbp
ret