How to access IP register? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to access IP register?

hanckjthanckjt Posts: 25Member
I want access the IP register.
I had tried "mov AX, IP" and "PUSH IP", but failed.
Can you tell me why and how to access it?
Thanks

Comments

  • 13yrassembler13yrassembler Posts: 13Member
    : I want access the IP register.
    : I had tried "mov AX, IP" and "PUSH IP", but failed.
    : Can you tell me why and how to access it?
    : Thanks
    :

    Unfortuantly, you cannot read from the IP register. The only thing you can do is change it, and you can accomplish that by jumping to another part of your code.
    [RED]Chris[/RED]

  • CroWCroW Posts: 348Member
    oh,yes,you can read the IP.not the direct-way,but with a little tricky code:

    [code]
    call NEAR PTR L1
    L1: pop ax ; <- IP of 'pop ax' is now in ax
    [/code]

    the 'call' opcode pushes the address of the NEXT command and jumps the the label.a NEAR call pushes only the IP (or EIP in protected mode),a FAR call also pushes CS.the 'ret' opcode retrieves the return-address from stack and jumps to it.

  • hanckjthanckjt Posts: 25Member
    : oh,yes,you can read the IP.not the direct-way,but with a little tricky code:
    :
    : [code]
    : call NEAR PTR L1
    : L1: pop ax ; <- IP of 'pop ax' is now in ax
    : [/code]
    :
    : the 'call' opcode pushes the address of the NEXT command and jumps the the label.a NEAR call pushes only the IP (or EIP in protected mode),a FAR call also pushes CS.the 'ret' opcode retrieves the return-address from stack and jumps to it.
    :
    :

    Oh, yeah!
    That's right!!
    Thanks.
  • Janib SoomroJanib Soomro Posts: 49Member
    ; call NEAR PTR L1
    ;L1: pop ax ; <- IP of 'pop ax' is now in ax
    ;
    ok, it right, but if you want to return back then you dont goback to where you come from!$!@#^&!@@#!@

    the simplest and easy way to get ip is!

    call near ptr routine1
    int 20h - exit
    routine1: push bp
    mov bp,sp
    mov ax,[bp+02]
    pop bp
    ret

    after this, ax=ip,
    in above code when you return from "L1" routine, your will land some where else, but in my code you will land +1 line of call instruction!
  • CroWCroW Posts: 348Member
    yes,you cant return.but I want to show a simple way to retrieve IP,without building a stackframe and 2 jumps (1st 'call',2nd 'ret')

    it just shows the trick behind,the low level-side.writing a 'real' subroutine returning IP in ax-register is useful for linking with c/c++ or pascal-code,but in those languages you should not need IP address...
    dont forget you have far-calls when linking assembly into c-programs.
    [code]
    ...
    call GetIP
    ...

    GetIP PROC
    pop ax
    sub sp,2
    ret
    GetIP ENDP
    [/code]

    something like that should work,too(im not sure,should try it):

    [code]
    mov ax,cs:L1
    L1: nop
    [/code]
  • Janib SoomroJanib Soomro Posts: 49Member
    : ...
    : call GetIP
    : line2:
    : ...
    :
    : GetIP PROC
    : pop ax - totally wrong
    : sub sp,2 - totally wrong
    : ret - wont return to exact line
    : GetIP ENDP

    No Try This Code I Wont Return To Line2: Procedure Or Dont Return To Routine [GetIp] + 1, [the next line]
  • CroWCroW Posts: 348Member

    'pop ax' does two things: it loads AX with the value from the stack and afterwards it ADDS 2 to sp.the 'sub sp,2' takes the stack back to the old point in memory.

    the stack is located at SS:SP.its just a memory-location like evrywhere else.you can change memory contents and SP aswell.the stack starts at higher offsets and gets to lower ones as more values are pushed on it.it doesnt matter how many values are put on the stack (or take from it) as long you have the same SS:SP at 'ret'-instruction as on entering your subroutine.
  • Janib SoomroJanib Soomro Posts: 49Member
    OOOOOOO, Im really sorry, i thought that pop ax will remove stack ip address, but it only subtract 2 from sp,
    Sorry! My Mistake!

Sign In or Register to comment.