Author Topic: RIP built in JWasm  (Read 3570 times)

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
RIP built in JWasm
« on: December 03, 2014, 07:45:52 AM »
I succeeded to build in RIP register :biggrin:
You must be aware of RIP usage
In disassembly you can not see RIP because it is not built in any disassembler
test it please
for now only 64 bit binaries:
« Last Edit: December 10, 2014, 12:54:22 PM by habran »
Cod-Father

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
Re: RIP built in JWasm
« Reply #1 on: December 03, 2014, 02:47:22 PM »
To get a 64 bit value of location pointed by RIP register use:
Code: [Select]
mov rax,[rip]
To get the address pointed by RIP register use:
Code: [Select]
lea rax,[rip]
here is how you can use RIP:
Code: [Select]
displacement EQU 200h

mov ah,[rip] 
mov rax,[rip+3]
mov rax,[rip+400h]
mov cx,[rip+128]
mov [rip+127],cx
mov [rip+displacement],rbx
mov rbx,[rip+displacement]
mov rax,[rip]
mov [rip+1],sil
cmp byte ptr [rip], 90h
lea rbx,[rip]
lea rax,[rip+2]
call qword ptr[rip+400]
push [rip]
push [rip+80h]
pop [rip]


« Last Edit: December 10, 2014, 01:17:43 PM by habran »
Cod-Father

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
Re: RIP built in JWasm
« Reply #2 on: December 03, 2014, 10:00:02 PM »
This is just a test, now my job is to prevent wrong usage and display errors
this means that I have to go through all possible commands that could use RIP register
when I finish I will upload source and give all possible examples when and how we can use it  8)
I wish I had source of WinDbg and Visual Studio Debuger, so that I can change it there as well :(
Cod-Father

anunitu

  • Member
  • ****
  • Posts: 896
Re: RIP built in JWasm
« Reply #3 on: December 05, 2014, 02:22:33 AM »
Did a search to understand RIP,so is it just in 64 bit mode?

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
Re: RIP built in JWasm
« Reply #4 on: December 05, 2014, 06:11:32 AM »
Yes anunitu, RIP is 64 bit register and RIP-relative addressing is available only in 64 bit
Cod-Father

anunitu

  • Member
  • ****
  • Posts: 896
Re: RIP built in JWasm
« Reply #5 on: December 05, 2014, 06:51:25 AM »
Haven't messed with 64 bit,but glad someone is looking into it. Is there an advantage to working in 64 bit?

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
Re: RIP built in JWasm
« Reply #6 on: December 05, 2014, 09:40:19 AM »
All new computers are 64 bit, even if there was no advantage you can not live in the past, but there is a great advantage:
  16 GPR registers an 8 of them nonvolatile (rbx,rbp,rdi,rsi,r12,r13,r14,r15)
  16 xmm registers     128 bit
  16 ymm register with AVX    256 bit
  32 zmm registers with AVX-512    512 bit (year 2015 I am looking forward :biggrin:)
  AVX-512 instructions also support for 32 SIMD registers in 64-bit mode (XMM0-XMM31, YMM0-YMM31 and    ZMM0-ZMM31).
 The number of available vector registers in 32-bit mode is still 8. 
As we know, CPU likes registers and dislikes a memory ;)
Cod-Father

anunitu

  • Member
  • ****
  • Posts: 896
Re: RIP built in JWasm
« Reply #7 on: December 05, 2014, 11:24:53 AM »
THAT is a LOT of registers. I still remember dos and trying a lot of tricks to get around so few regs..

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
Re: RIP built in JWasm
« Reply #8 on: December 05, 2014, 02:34:26 PM »
That's why I liked Motorola processors before :biggrin:
Cod-Father

TWell

  • Member
  • ****
  • Posts: 748
Re: RIP built in JWasm
« Reply #9 on: December 05, 2014, 05:19:52 PM »
This example from here seems to work with that JWasm:
Code: [Select]
.model flat

extern MessageBoxA: PROC
extern ExitProcess: PROC

.data
testok    db 'RIP based addressing worked!', 0
testfail  db 'RIP based addressing failed at test '
testcode  db '0'
          db '!', 0
testtitle db 'RIP based addressing test by Fibergeek', 0

.code

public Main

Main:
  sub rsp, 40
  ; Put the value of the NOP opcode in R8B
  mov r8b, 90h

  ; Assume that the test failed
  lea rax, testfail

  ; Test 1
  ; NOTE: i'm hardcoding this instruction because of ML64
  inc testcode
  cmp [rip], r8b
  ;DB 44h, 38h, 05h, 00h, 00h, 00h, 00h
  nop
  jne done

  ; Test 2
  ; NOTE: i'm hardcoding this instruction because of ML64
  inc testcode
  lea rbx, [rip]
  ;DB 48h, 8Dh, 1Dh, 00h, 00h, 00h, 00h
  nop
  cmp [rbx], r8b
  jne done

  ; Test 3
  inc testcode
  call $ + 5
  nop
  pop rbx
  cmp [rbx], r8b
  jne done

  ; Test 4
  inc testcode
  call $ + 5
  pop rbx
  add rbx, 1 + 1 + 2 + 1 ; 1=POP, 1=REX, 2=ADD, 1=imm8
  nop
  cmp [rbx], r8b
  jne done

  ; All tests succeeded
  lea rax, testok

done:
  ; Display the result and exit the program
  mov r9d, 0         ; R9D = UINT uType
  lea r8,  testtitle ; R8  = LPCTSTR lpCaption
  mov rdx, rax       ; RDX = LPCSTR lpText
  mov rcx, 0         ; RCX = HWND hWnd
  call MessageBoxA
  mov ecx, eax       ; ECX = UINT uExitCode
  call ExitProcess

  ; Just in case :)
  ret
END Main

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
Re: RIP built in JWasm
« Reply #10 on: December 05, 2014, 09:35:02 PM »
Hi TWell :biggrin:
Thank you for testing :t
There is no doubt that it works fine
All I have to do yet is to prevent wrong usage
thank you for the link, I'll look it up later tonight
Cod-Father

Gunther

  • Member
  • *****
  • Posts: 3515
  • Forgive your enemies, but never forget their names
Re: RIP built in JWasm
« Reply #11 on: December 06, 2014, 11:09:12 PM »
Hi TWell,

good link. Thank you for providing it.

Gunther
Get your facts first, and then you can distort them.

TWell

  • Member
  • ****
  • Posts: 748
Re: RIP built in JWasm
« Reply #12 on: December 06, 2014, 11:19:07 PM »
Can someone explain that RIP / PIC thing, what that actually is?
Less relocations, but how?

anunitu

  • Member
  • ****
  • Posts: 896
Re: RIP built in JWasm
« Reply #13 on: December 06, 2014, 11:39:52 PM »
Link from Intel on 64 bit programming in assembler.

https://software.intel.com/en-us/articles/introduction-to-x64-assembly

habran

  • Member
  • *****
  • Posts: 1107
    • uasm
Re: RIP built in JWasm
« Reply #14 on: December 10, 2014, 01:02:48 PM »
I have uploaded a new version with all possible usage (I hope so)
Please test it and if you are happy I'll post the source as well 8)
Cod-Father