assmbly quiz

Discussion in 'Assembly Language Programming (ALP) Forum' started by adam2009, Jul 10, 2009.

  1. adam2009

    adam2009 New Member

    Joined:
    Jul 10, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    hey all,

    I have some questions in assembly 80x86
    and i wondered if anyone could give me a hand...
    (got only the first q right...)


    many thanks
    Adam
     

    Attached Files:

  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The general idea of homework questions is that you do them yourself. Failure to do them correctly is a good indicator to your teacher that you're struggling so we do you no good by answering them for you - you hand in correct answers, he thinks "good he's got the hang of this", but in reality you haven't and by answering your questions we only succeed in creating a dependency cycle where rather than you figuring stuff out, you just post your questions here for others to answer for you.

    Which is fine if you get answers, but on the day of the exam where you have no access to go4e, you're stuck because you haven't answered questions yourself before, and you fail.

    However I don't think it's a problem to give you some hints, so here goes:

    1. You got that correct, good. Did you work it out yourself or get the answer from someone? You only fool yourself when you cheat, because the point of these exercises is not just to give you a hard time but actually to teach you stuff. Do you think that if the best way to teach programming were "here look at this complete code sample", "here now look at this complete code sample", your teacher wouldn't already be doing it this way?

    2. I believe the x86 has some "repeat" instructions that perform loop operations without you having to code up a loop. Dig through your course notes and see what you can find.

    3. OK so you have 3 variables in (this is important) FOUR CONSECUTIVE BYTES. Is there something about the phrase FOUR CONSECUTIVE BYTES that suggests a possible way ahead for this? Look carefully at the "assume" bit in brackets; ask yourself the question "does this give you the answer".

    4a. Well, what do you think the result is? There's no point any of us just telling you outright. Have a go and explain how you got the value you got, and it is the working that is important here because we can work out if your TECHNIQUE is correct, so for example there's no point just posting, say, "27" because if that's the wrong answer we don't know how you arrived at that.

    If you have no idea how, just take the instructions one at a time. What does MOV AX,0 do (bearing in mind the stated assumption)? Next: what does ADD AX, BX do and what is the result of that? One the basis of that result, what then would ADC DX,0 do? And so on. This is called "dry running" the code and is a useful programming skill; you NEED to get this sorted out. Really you do. I've been programming since 1981 and I still dry run code in my head.

    4b. Having worked out the result, now have a go at solving the problem yourself. If you get stuck, post the code and we can suggest what to do next.
     
    mayjune likes this.
  3. mayjune

    mayjune New Member

    Joined:
    Jun 14, 2009
    Messages:
    814
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Pune,Delhi
    i like the way you answered ...
     
  4. adam2009

    adam2009 New Member

    Joined:
    Jul 10, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    first i'm sorry you have got the wrong impression from my post. I should have attached my dead-end draft. the thing is that i wrote it not in english and i was too lazy to translate it.

    i have translated it shortly, just to show that i did try by myself and got stucked



    Question 1
    external atoi

    %macro print_num 1

    mov dword[NUMBER], %1
    //push NUMBER
    //call atoi
    //pop eax
    // mov dword[NUMBER], eax //what if number is too big?? The result is moved to eax automaticall?
    mov eax,0X04
    mov ebx,0x01
    mov ecx, NUMBER
    mov edx 0x020 //32 decimal bytes
    int 0x80
    //can’t use labels to error printing
    %endmacro


    Question 2
    The code checks how many ones are in ebx

    ?? there must be a test whether we searched the whole ebx

    Byte masking

    Calling func?
    Doing 32 times

    Question 3.1
    mov ax, 0x01
    inc ax //assuming ax is 0
    add ax, 0x01 //assuming ax is 0
    or ax, 0x01 //assuming ax is 0

    Question 3.2
    STC
    Shr
    ?? not good not sure the other digits are zero

    Q4
    ?? is there any catch ?
    add y, 0x05 //canot add to memory?
    sub x, 0x02
    inc dword[z]
    //suopposed to be longer…


    Q5.1
    //-no- carry only in 32 bytes--// Adds cx-1 times the carry from bx*2
    Adds the previous carry bit to DX cx times (doest change during calculation)

    Q5.2
    ??

    times ecx
    times ecx adc Dx, 0

    secondly, thatnk for you sincere hlep. i''l look throgh the assigenment again. hope to manage...
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    OK, there are 2 question 2's - that's confusing.
    Taking the second 2, plus 3,4 in the PDF as 3,4,5 this makes more sense...

    Q4:
    Yep, you could use three instructions, but the question asks for the shortest possible way.
    I dropped a major hint twice, see if you can spot it.

    FOUR CONSECUTIVE BYTES
    FOUR CONSECUTIVE BYTES
    FOUR CONSECUTIVE BYTES
    FOUR CONSECUTIVE BYTES
    FOUR CONSECUTIVE BYTES
    FOUR CONSECUTIVE BYTES
    FOUR CONSECUTIVE BYTES
    FOUR CONSECUTIVE BYTES
    FOUR CONSECUTIVE BYTES
    FOUR CONSECUTIVE BYTES

    Dropped it 10 more times for you just in case you couldn't find it.

    I wonder if there are any 4 byte instructions.
    I wonder if we can somehow combine -2 in one byte, +1 in 2 bytes and 5 in one byte into a single four-byte constant, cos if we can, and if there are 4-byte instructions, then maybe we can solve this with a single instruction.
    I wonder if the "assume" bit in brackets has anything* to do with a possible answer.
    * maybe this is the answer handed to you on a silver platter.

    5.1
    No. Try dry running the code. Assume some (small - to keep the simulation short) values for BX and CX. Have an 8086 reference at hand, for example http://www.emu8086.com/assembly_language_tutorial_assembler_reference/8086_instruction_set.html
    Let's say BX, CX, DX start out at 5,4,0 respectively. You might find it helpful to write it out on paper in tabular form. This is an absolutely essential skill for programmers, by the way. I'll get you started:
    Code:
    Instruction   AX  BX  CX  DX
    
    start:        ??   5   4   0
    mov ax,0       0   5   4   0
    add ax,bx *1   5
    adc dx,0  *2 (not filled in - you must RTFM)
    loop                   3      CX!=0 so we loop back to the "add"
    add ax,bx     10
    adc dx,0  (not filled in - you must RTFM)
    loop                   2      CX!=0 so we loop
    ...
    
    Some notes:
    *1 Look this up in the documentation. What would happen if AX+BX>65535? Whatever the outcome, are you certain this doesn't affect the carry bit at all (as per your comment "Adds the previous carry bit ... (doesn't change during calculation)")?
    *2 Also look this up in the documentation. What's the difference between ADD and ADC? If AX+BX<65536, what would the ADC instruction do? If AX+BX>65535, would the ADC instruction do anything different? I haven't put values under AX~DX for this instruction, deliberately, because you need to lookup ADC in TFM.

    Once you've done that, do it again with a big starting value for BX - say 50,000. Use a calculator to keep track of BX.

    Note: with experience you'll be able to dry run stuff in your head. Dry running on paper first is an essential first step, only when you've done that a few times will you be able to do it mentally, just like maths, there was a time you could only do moderately complicated maths on paper, e.g. 62*120, but I bet you could do that in your head now.

    5.2. It's very interesting that the DX register was chosen. I wonder if there's a single instruction that does what the previous code chunk does? (That's why you have to get what it does correct). Perhaps the choice of the DX register is a clue. But if I tell you where to look in the docs then I give away what the first part does, and you need to work that out.
     
  6. adam2009

    adam2009 New Member

    Joined:
    Jul 10, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Wow ! that you so much for the comprehensive answer.

    I can't ask you for more guidance but if you are able to give a hand. i'll be more that happy to receive it.

    I attached
    1. the original assignment, because last time the number got mixed up. now i will be sure which question your referring to.
    2. my draft of answers (not asking anyone to look at it, just to show i'm trying myself)
    3. an answer to your last post.

    best regards
    Me
     
  7. adam2009

    adam2009 New Member

    Joined:
    Jul 10, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    the files:
     

    Attached Files:

  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > Note: This assignment is to be done SOLO

    Hmm. So why are you posting it here then?
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > I thought about that too. I believe you refer to reserved-word "times". That problem is that it loops only one line, and I can't make a proper instruction in one line only. Or am I missing something here?

    Yes you are. Try searching the documentation link I gave you for the word "repeat". What do you find at the FIRST match?

    > Q3 in PDF. For that I really don't have a clue. I have tried some things but there possible only if I assume EAX is initialized 0.any more ideas?

    What you need to do here is look through each instruction in the documentation and find five that are guaranteed to leave 1 in AX. MOV AX,1 is the obvious starting point; now find four more!

    > thanks. Solving that have raised up some technical questions. When I'm adding a number to a data in some memory say in address 100, will the addition direction be to the right or to the lest (meaning the adding the MSB I summed with data in memory 100,next is summed with data in memory 101… or LSB with 100, next bit with data in address 99…)

    That's a very good question. What you need to do is read the documentation carefully, and/or try it out on an x86 machine. There are loads of emulators available if you haven't got an actual machine handy. But you have the right approach, and now you just need to work out whether the address to point the instruction at is 100 or 103.

    > I don't get it.

    For the THIRD time -- listen very carefully because I'm only going to say this once more and after that you're on your own:

    TRY DRY RUNNING THE CODE.

    I got you started. Now do it. Even if just to humour me, you never know, you might just be surprised at what you find.

    > 5.2

    We need to come back to this AFTER you have dry run the code in 5.1. Once you know what 5.1 does, you can then look for an instruction that does the same.
     
  10. adam2009

    adam2009 New Member

    Joined:
    Jul 10, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    thanks.

    one last question:
    do you know a good debugger with which i can see that data un a given memory address as well ?
     
  11. adam2009

    adam2009 New Member

    Joined:
    Jul 10, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0

    another final q:
    Write 80X86 instructions to get a value of 1 in the ax register on an 80X86:

    1. List 5 different ways to do this with ONE instruction.


    i have found only 4

    mov ax, 1
    mov eax,1
    div ax
    idiv ax

    ??
     
  12. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I don't know - anyone else got any ideas?
    Won't div ax throw an exception if ax=0? Ideally ax/ax should always equal 1, but 0/0 is mathematically undefined.
     
  13. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What about lea - is there a variant of that which will have the desired effect?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice