2014-12-28 9 views
1

.text은 읽기 전용이므로 어셈블리에서 자체 수정 코드가 가능한지보고 싶었습니다. 대신 코드를 .data에 넣을 수 있다고 생각했지만 아래 코드를 시도했습니다. 자체를 수정 한 후, 대신 증가 명령의 감소 지시를 가져야 때문에 I는 2 예상되지만이 어셈블리 코드가 자체적으로 수정하지 않는 이유

section  .text 
global  _start       ;must be declared for linker (ld) 

_start:          ;tell linker entry point 

    mov al,3 
    mov rbx,cont1        ;store label cont1 in rbx so we can continue there 
    mov cl,byte [dec_op]      ;put the opcode of the dec instruction in cl 
    mov byte [code],cl      ;replace the inc instruction with a dec instruction 
    jmp code         ;run the modified code 
cont1: 
    mov byte [dat1],al      ;store away the changed value 
    add byte [dat1],0x30      ;turn the internal numeric value into an ascii digit 
    mov eax,4         ;system call number (sys_write) 
    mov ebx,1         ;stdout 
    mov ecx,dat1        ;location of data 
    mov edx,2         ;length of data 
    int 0x80         ;call kernel 

    mov  eax,1        ;system call number (sys_exit) 
    int  0x80        ;call kernel 

section  .data 

dat1  db 0,0xa       ;location to store output 
dec_op  dec al       ;the dec instruction 
code  inc al       ;code to modify 
      jmp rbx 

위의 출력

4 

이다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

내가 데비안 SID 64 비트를 실행하는거야 내가 컴파일 해요 :

nasm -f elf64 sm.s 
ld -s -o sm sm.o 
+0

http://en.wikipedia.org/wiki/Self-modifying_code#Interaction_of_cache_and_self-modifying_code –

+0

@HansPassant 그 의미가, 당신은 캐시를 syncronize하는 데 필요한 지침에 모든 자원을해야합니까? –

+0

올바른 양식의 thje 자체 수정 코드를 수행하고 있습니다. 수정을 한 후에 jmp를 사용하면 프리 페치를 지우는 데 필요한 모든 작업을 수행 할 수 있습니다. – Jasen

답변

4

DEC AL은 NASM에 의해 생성되는 "listfile을"에서 2 바이트

이 걸리기 때문에 그것의 (열이 약간 응축) :

26 00000000 000A  dat1  db 0,0xa    ;location to store output 
27 00000002 FEC8  dec_op  dec al     ;the dec instruction 
28 00000004 FEC0  code  inc al     ;code to modify 
29 00000006 FFE3     jmp rbx 

두 op 코드의 첫 번째 바이트는 동일하므로 혼란이 있음을 알 수 있습니다.

그리고 수정되었습니다.

mov cx,word [dec_op]      ;put the opcode of the dec instruction in cl 
    mov word [code],cx      ;replace the inc instruction with a dec instruction 
관련 문제