Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

walters

  • one year ago

how to write this in assembly while(x>1) { if(x is even) x=x/2 }

  • This Question is Closed
  1. slotema
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    The basic structure of a while loop is also shown here: http://openstudy.com/study#/updates/5124d00be4b086b98ebdbc10 The loop body can be done in two ways: slow and faster. I'll explain the slow method in more details and I'll give some hints for the faster method. For the slow method, you'll need a division. Divisions can be a bit complex to use. You'll need to put the dividend into two registers: the upper half in edx and the lower half in eax. Then you can use the div instruction with the divisor as argument. The quotient will be in eax and the remainder in edx. So in code. Let's assume x is an unsigned int (so the upper half is all 0s) and is located in esi: mov edx, 0 mov eax, esi mov edi, 2 # div only works on registers and # memory locations div edi cmp edx, 1 jnz endif # if the compare is zero, the number # is even so we need to jump when # it's not mov esi, eax # eax is still the result of the # previous division. so no need # to redo it endif: ... For the fast method, you can check certain bits to see if a number is even/odd. Also, division by a power of 2 can be replaced with a fast instruction.

  2. slotema
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Probably better for the comparisson: cmp edx, 0 jne endif

  3. walters
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    is it going to loop until the number is eqqual or less than 1.Or it will only show register output once?

  4. slotema
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    That depends on the initial value of x and how you implement the while loop. The code I posted above will not loop.

  5. walters
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    so if i am using the while loop everything will be like this(L1:................loop L1) ?

  6. slotema
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    What do you mean? Do you have some ASM?

  7. walters
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    YES

  8. walters
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    1 Attachment
  9. slotema
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Yes, you can use the loop instruction, but make sure that the loop counter in in %ecx (you seem to check %eax at the start of the loop)

  10. walters
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    but it does not loop

    1 Attachment
  11. slotema
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    For me, it gets into an infinite loop because of the jnz L1. You should probably update %eax somewhere.

  12. Not the answer you are looking for?
    Search for more explanations.

    Search OpenStudy
    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.