PLEASE HELP ME TO SOLVE THIS PROBLEM ASSEMBLY LANGUAGE Create C/C++ Win32 Consol
ID: 3928555 • Letter: P
Question
PLEASE HELP ME TO SOLVE THIS PROBLEM
ASSEMBLY LANGUAGE
Create C/C++ Win32 Console Application solution in the Microsoft Visual Studio;
Task 1) Count the number of positive values
Task 2) Calculate the total of positive values
Apply IA-32 instructions and use the following template as a base for the solution (a working application with successful output). * If the required value does not exist, then you must return -1.
// For each task level variant implement own function, e.g.:
int solution_for_task_X(const int arr[], size_t arr_size)
{
int result = 0;
__asm
{
// Your Inline Assembler instructions for grade X level go here
// :::
mov [result], eax ; save the result
}
return result;
}
// :::
int main()
{
int test_result;
// Change the element count and values according to your algorithm:
int test_arr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
test_result = solution_for_task_X(test_arr1, sizeof(test_arr1)/sizeof(test_arr1[0]));
// You can use assert() function to test for correctness:
assert(expected_result == test_result);
// Or print the results to console:
printf("Task 1 result = %d ", test_result);
// For the same task function you might use different input arrays,
// e.g. to test for side cases:
int test_arr2[] = { 0, -1, 2, -3, 4, -5, 6, -7, 8, -9 };
test_result = solution_for_task_X(test_arr2, sizeof(test_arr2)/sizeof(test_arr2[0]));
assert(another_expected_result == test_result);
// Or print the results to console:
printf(" Task 2 result = %d ", test_result);
// :::
return 0;
Explanation / Answer
int solution_for_task_X(const int arr[], size_t arr_size){
int result = 0;
--asm
{
sw $0,0($fp)
sw $0,4($fp)
$L4:
lw $3,4($fp)
lw $2,20($fp)
sltu $2,$3,$2
beq $2,$0,$L2
nop
lw $2,4($fp)
sll $2,$2,2
lw $3,16($fp)
addu $2,$3,$2
lw $2,0($2)
blez $2,$L3
nop
lw $2,4($fp)
sll $2,$2,2
lw $3,16($fp)
addu $2,$3,$2
lw $2,0($2)
lw $3,0($fp)
addu $2,$3,$2
sw $2,0($fp)
$L3:
lw $2,4($fp)
addiu $2,$2,1
sw $2,4($fp)
b $L4
nop
mov [result], eax ; save the result
}
return result;
}
int main()
{
int test_result;
// Change the element count and values according to your algorithm:
int test_arr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
test_result = solution_for_task_X(test_arr1, sizeof(test_arr1)/sizeof(test_arr1[0]));
// You can use assert() function to test for correctness:
assert(expected_result == test_result);
// Or print the results to console:
printf("Task 1 result = %d ", test_result);
// For the same task function you might use different input arrays,
// e.g. to test for side cases:
int test_arr2[] = { 0, -1, 2, -3, 4, -5, 6, -7, 8, -9 };
test_result = solution_for_task_X(test_arr2, sizeof(test_arr2)/sizeof(test_arr2[0]));
assert(another_expected_result == test_result);
// Or print the results to console:
printf(" Task 2 result = %d ", test_result);
// :::
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.