Link to home
Start Free TrialLog in
Avatar of reluh
reluh

asked on

ASM problem 2

Can anybody please explain me what is happening in here?

mov sp,1111h
mov bp,2222h
mov word ptr [bp],3333h
push bp
xchg bp,sp
pop bp

Thank you
Avatar of Infinity08
Infinity08
Flag of Belgium image

mov is the move instruction - it copies the second argument into the first.
push is the push instruction - it pushes the argument onto the stack (incrementing the stack pointer in the process).
pop is the pop instruction - it pops a value from the top of the stack into the argument (decrementing the stack pointer in the process).
xchg is the exchange instruction - it swaps the two arguments around.

sp is the stack pointer - it points to the current top of the stack.
bp is the base pointer - it points to the start of the current stack frame
Avatar of reluh
reluh

ASKER

Can you please tell me which shoul dbe the the value of BP?
Are you asking what the value of bp will be after running this code ?

Is this for an assignment of yours ?
Avatar of reluh

ASKER

Yes this is what I want . The value of bp after running this code. It is not a homework. I want to try some things and it is the first time I use asm.
If this is just something you tried, then I can tell you that the code is modifying the stack pointer and base pointer in a non-standard way. This is not the kind of code you should be writing, because you're only teaching yourself the wrong habits ...

If this is an assignment however (as I suspect), it makes more sense (and I'd be able to assist you in solving it). But since you say it isn't ... there's not much more I can say than recommend against using code like this.
Avatar of reluh

ASKER

ok all I need to know is the value of BP. I need it to understand the flow.
It would make more sense to figure out the "flow", and find out what the value of bp is based on that understanding.

But then it seems we're back to the assignment assumption ... so I'll assume it is.

Given my first post here, what is your understanding of what happens for each instruction ? Could you post an explanation of what you think happens here ?
Avatar of reluh

ASKER

ok!

1. copy the 1111h value to sp
2. copy the 2222h value to bp
3. copy the 3333h representation on 2 bytes starting from bp address
4. push bp add it to the stack and decrease the sp with 2
5  exchange value from sp to bp
6  get te bp from the stack and increase with 2

I think that bp value can not be determined in this case.

Thank you
The gist of it is correct, but there are some details that need refinement. For example, the push and pop instructions depend on the current value of sp.

Could you augment your explanation of the instructions with the values of bp and sp after executing each of the instructions ?
Avatar of reluh

ASKER

mov sp,1111h sp = 1111h
mov bp,2222h bp = 2222h
mov word ptr [bp],3333h bp = 3333h - at this poitn I am not sure what is happen with this value
push bp
xchg bp,sp
pop bp
>> mov sp,1111h sp = 1111h
>> mov bp,2222h bp = 2222h

Correct.

>> mov word ptr [bp],3333h bp = 3333h - at this poitn I am not sure what is happen with this value

word ptr [bp] refers to the memory location that has the address currently stored in bp. So this move instruction copies a value to a certain memory location, not to the bp register.
Avatar of reluh

ASKER

an this is the reason that the bp can not be determined
>> an this is the reason that the bp can not be determined

The third instruction does not change bp. It changes something else. So, after the third instruction, bp still has the same value as it had before the instruction.
Avatar of reluh

ASKER

Yes you are right. So
push bp bp= 2222h
xchg bp,sp bp= 110Fh
pop bp bp = 2222h

Is that right?
Almost.

Remember what I said about the fact that push and pop depend on the current value of sp ? Both instructions will use sp to know where the current top of the stack is, and will modify sp after performing the push resp. pop operation.

Make sure that you keep in mind what the sp value is the moment these two instructions are executed.
Avatar of reluh

ASKER

push bp bp= 2222h sp = 1111h
xchg bp,sp bp= 110Fh sp = 2222h
pop bp bp = it canot be determined because the sp is 2222h instead of 110Fh

Is it ok now :(
>> pop bp bp = it canot be determined because the sp is 2222h

What does that pop instruction actually do ?

(a) it gets the value at the address indicated by sp
(b) it stores that value in bp
(c) it adjusts sp
Avatar of reluh

ASKER

So bp is 2222h
If sp is 2222h, which value does pop store in bp ?
Or in other words : which value is stored at address 2222h ?
Avatar of reluh

ASKER

I don't know! Please tell me. I am loosing something.
Have a closer look back at the third instruction. Remember how it stored a value at a certain memory address ?
Avatar of reluh

ASKER

2222h
>> 2222h

That's the memory address, yes. What value was stored there ?
Avatar of reluh

ASKER

I realy don 't know
This the third instruction :

>> mov word ptr [bp],3333h

it takes the value from bp, interprets it as a memory address, and stores the value 3333h at that address.

So, what value is stored at the memory address 2222h ?
Avatar of reluh

ASKER

3333h- NOW I understood!
>> 3333h- NOW I understood!

Right. So, which value is stored in bp after executing all 6 instructions ?
Avatar of reluh

ASKER

3333h
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of reluh

ASKER

Thank you very much for you help.
No problem. I'm glad I could be of assistance :)