Question

In: Computer Science

I want to convert those codes to assembler code // Problem 1 // for loop J=5...

I want to convert those codes to assembler code

// Problem 1

// for loop

J=5

for(i=1; i<5; i++) {

        j--

}

// Problem 2

// if - then - else

i=4

if (i < 5) then

        j = 3

else

        j = 2

// Problem 3

//while loop

i = 0

j = 0

while(i==0) {

  j++

  if j = 5 then

        i = j

}

Solutions

Expert Solution

1) Simply we write mov instruction for assigning values to a variable.

Mov instruction : mov destination,source.

Given problem :
//for loop
j=5
for(i=1; i<5; i++) {
        j--
}

Given code can be converted to assembley code as follows :

pushq    %rbp
    movq    %rsp, %rbp
    movl    $5, -8(%rbp) ;j=5
    movl    $1, -4(%rbp) ;i=1
    jmp    .L2
.L3:
    subl    $1, -8(%rbp)
    addl    $1, -4(%rbp)
.L2:
    cmpl    $4, -4(%rbp) ;condition check - i<5
    jle    .L3
    popq    %rbp
    ret



2) Given problem :

i=4
if (i < 5) then
        j = 3
else
        j = 2

This code can be converted to assembley code as follows :

    pushq    %rbp
    movq    %rsp, %rbp
    movl    $4, -8(%rbp) ;assignment instruction which is equivalent to i =4i=4
    cmpl    $4, -8(%rbp) ;comparing whether i < 5 or not
    jg    .L2
    movl    $3, -4(%rbp)
    jmp    .L3
.L2:
    movl    $2, -4(%rbp)
.L3:
    popq    %rbp
    ret

3) Given code :

i = 0
j = 0
while(i==0) {
j++
if j = 5 then
        i = j
}

Above code can be converted to assembley code as follows :

    pushq   %rbp
   movq   %rsp, %rbp
   movl   $0, -8(%rbp)
   movl   $0, -4(%rbp)
   jmp   .L2
.L3:
   addl   $1, -4(%rbp) ;increment of j
   cmpl   $5, -4(%rbp)
   jne   .L2
   movl   -4(%rbp), %eax
   movl   %eax, -8(%rbp)
.L2:
   cmpl   $0, -8(%rbp) ;while condition check
   je   .L3
   popq   %rbp
   ret
   
   


Related Solutions

this is my code I want the opposite i want to convert a postfix expression to...
this is my code I want the opposite i want to convert a postfix expression to infix expression #include <iostream> #include <string> #define SIZE 50 using namespace std; // structure to represent a stack struct Stack {   char s[SIZE];   int top; }; void push(Stack *st, char c) {   st->top++;   st->s[st->top] = c; } char pop(Stack *st) {   char c = st->s[st->top];   st->top--;   //(A+B)*(C+D)   return c; } /* function to check whether a character is an operator or not. this function...
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
1. Convert the following pseudo code routine into MIPS assembler language: - Set register $t0 =...
1. Convert the following pseudo code routine into MIPS assembler language: - Set register $t0 = 1 - In four separate instructions add 2, then 3, then 4, then 5 into register $t0 $t0 = $t0 + 2 + 3 + 4 + 5 - Copy the result from register $t0 into register $t1 2. Convert the following into MIPS assembler language: - Set register $t0 = 0 - Initialize the register $t1 = 10 - Use register $t2 as...
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert while loop to for loop example 2.) pass one dimension array(and its size) to function example
Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding...
Write assembler code to print even numbers from 1-20. Submit code and screenshot. I am coding in NASM on a SASM system.
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
Document the arrays, using a chart with columns titled: outer loop, inner loop, i, j, and...
Document the arrays, using a chart with columns titled: outer loop, inner loop, i, j, and x. Finally, draw a picture of each array (after the program portion has executed). (5 pts) int[][] arr1 = new int[5][5]; int[][] arr2 = new int[5][5]; x = 1; for(int i = 0; I < 5; i++) { for(int j = 1; j < 6; j++) { arr1[i][j-1] = x; x++; if(x == 6) x += 2; } } for(int i = 4; i...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
Write the following code in ARM assembly code g=12, h=8, i=2, j=5; f = (g +...
Write the following code in ARM assembly code g=12, h=8, i=2, j=5; f = (g + h) - (i + j); Your program displays the message: f = (g + h) – (i + j) = 13 Note that 13 should be calculated, not hardcoded
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT