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); }
```please convert this code to make only using for loop not while loop #include #include #define...
```please convert this code to make only using for loop not while loop #include #include #define MAX_SIZE 500 int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; char part1[100]; char part2[100]; int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0; int j = 0; int a = 0; int b = 0; int total = 0; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); for (i = 0; i < strlen(tosearch); i++) {...
```please convert this code to make only using for loop not while loop #include #include #define...
```please convert this code to make only using for loop not while loop #include #include #define MAX_SIZE 500 int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; char part1[100]; char part2[100]; int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0; int j = 0; int a = 0; int b = 0; int total = 0; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); for (i = 0; i < strlen(tosearch); i++) {...
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.
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
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT