Interrupt 10h not working

后端 未结 2 1267
我在风中等你
我在风中等你 2020-12-22 01:58

I am getting segmentation fault in the program below.
This is for set the cursor on the top-left of the screen. But why i am getting segmentation fault on this program?

2条回答
  •  伪装坚强ぢ
    2020-12-22 02:22

    Try the following:

    org 100h
    section .text
    global main
    main:
        mov ah, 2
        mov bh, 1
        mov dh, 0
        mov dl, 0
        int 10h
    

    Please read this:

    This org 100h actually tells assembly that our program will begin at offset 100h. Why is this necessary? It is because all running programs have Process Control Block (PCB) with it. It's sort of thing for operating system to manage stuffs, So, it's better for us not to interfere with that unless you're doing advanced stuff. After that, we have a jump, right? Then after that jump, you put all your data in, right? That's how we cope with this chaos. The unconditional jump ensures the space for data so that it does not interfere with code. and vice versa. It is usually the case when the code interferes with data, it will cause hangups, blue screen of death and so on, --again -- UNLESS you are an assembly guru that knows what you're doing (like doing some self-modification code stuff and similar arcane tricks).


    For linux you should work only with system calls this is a well documented tutorial, you put in eax the number of the sys call and jump to it/ switch into kernel mode with int 80h:

    section .data
        hello:     db 'Hello world!',10    ; 'Hello world!' plus a linefeed character
        helloLen:  equ $-hello             ; Length of the 'Hello world!' string
                                           ; (I'll explain soon)
    
    section .text
        global _start
    
    _start:
        mov eax,4            ; The system call for write (sys_write)
        mov ebx,1            ; File descriptor 1 - standard output
        mov ecx,hello        ; Put the offset of hello in ecx
        mov edx,helloLen     ; helloLen is a constant, so we don't need to say
                             ;  mov edx,[helloLen] to get it's actual value
        int 80h              ; Call the kernel
    
        mov eax,1            ; The system call for exit (sys_exit)
        mov ebx,0            ; Exit with return code of 0 (no error)
        int 80h
    

提交回复
热议问题