Author Topic: Encoding Viewer  (Read 1612 times)

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Encoding Viewer
« on: December 02, 2016, 01:57:01 PM »
Simple program below will enable you to view an encoding for one instruction. This is useful if you want to quickly verify and confirm the code generated by the assembler even if your current CPU doesn't support that instruction you're testing. For example, the code below shows the precise encoding for VPERMQ instruction even if my CPU doesn't have AVX512.

Code: [Select]
;compile  : nasm -f win64 this.asm
;Link     : golink /console /entry _start this.obj msvcrt.dll

global _start

foo:
        ;instruction to encode here
        vpermq zmm0,zmm1,3  ;AVX512
bar:

section .text
_start:
        mov     rsi,foo
        mov     rdi,bar
        xor     eax,eax
        sub     rdi,rsi
.more:  lodsb
        mov     rcx,fmt
        mov     rdx,rax
        sub     rsp,0x20
        call    [printf]
        add     rsp,0x20
        sub     rdi,1
        jnz     .more
        mov     rcx,0
        call    [exit]

section .data
fmt db '%02X ',0

extern printf
extern exit

Just put one instruction between foo and bar. You can modify the program to suit your needs but foo and bar must be put out of the execution path. I don't have my Linux machine right now but similar effect can be achieved using opcode routine from my BASELIB library for Linux.

Happy disassembling :D
« Last Edit: December 02, 2016, 02:16:16 PM by stressful »

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: Encoding Viewer
« Reply #1 on: December 06, 2016, 12:54:58 PM »
Holiday mood right now... Here's the code for Linux (but you probably have figured it out already)

Code: [Select]
;compile: nasm -f elf64 this.asm
;Link   : gcc this.o -o this
;-------------------------------
global main

foo:
        ;lfence
        ;mov cr0,rax
bar:

section .text
main:
        enter 0,0
        mov     rsi,foo
        mov rbx,foo
        mov     rdx,bar
        sub     rdx,rsi
        jz .done
.more: xor esi,esi 
mov     sil,[rbx]
        mov     rdi,fmt
        inc rbx
        push rdx
        push rbx
        call    printf
        pop rbx
        pop rdx
        sub     rdx,1
        jnz     .more
        mov rdi,0ah
        call putchar
.done: leave 
ret
       
section .data
fmt db '%02X ',0

extern printf
extern putchar

Added advantages, in addition to the first post;

1. You can see the encoding of almost all system instructions and registers that are otherwise prohibited in normal mode
2. You can test what instructions are currently (not) supported by NASM, as long as you give the correct syntax
3. Gives you instruction size.
4. Boss-ing around NASM authors by showing them 'faulty' encoding even if you are completely clueless yourself. (not recommended. NASM people are not that funny)

But hey, enjoy you holidays.