Interrupt 10h not working

后端 未结 2 1265
我在风中等你
我在风中等你 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:29

    BIOS interrupts are 16-bit code. Your OS has put the CPU in 32-bit protected mode. The hardware will allow a switch back to 16-bit real mode (there are hoops to jump through) but the OS won't allow it. Wouldn't be very "protected" if it did. It is "protected" from US, my friend!

    I think what you probably want to look into is "vt100" terminal emulation. By rights, a "robust" program would consult the "termcaps" file to make sure vt100 emulation is available before attempting to use it. My experience is that it's "usually" available on a "desktop Linux" box, so I just ASSume it's there. Worst that can happen (I think) is garbage on the screen if we ASSume wrong.

    This example doesn't do exactly what you want. It saves the current cursor position (lord knows where), moves the cursor to a new position, prints a message, and goes back to the original cursor position. You'll need to look up the "home cursor" command ("ESC [h"? lookitup). Just write it to stdout, same as "hello world". You can get colors and stuff, too.

    ; nasm -f elf32 mygem.asm
    ; ld -o mygem mygem.o -melf_i386
    
    global _start
    
    section .data
    savecursor db 1Bh, '[s'
    .len equ $ - savecursor
    
    unsavecursor db 1Bh, '[u'
    .len equ $ - unsavecursor
    
    getcursor db 1Bh, '[6n'
    .len equ $ - getcursor
    
    setcursor db 1Bh, '[10;20H'
    .len equ $ - setcursor
    
    msg db "Hello, new cursor position!"
    .len equ $ - msg
    
    section .text
    _start:
    
    mov ecx, savecursor
    mov edx, savecursor.len
    call write_stdout
    
    
    mov ecx, setcursor
    mov edx, setcursor.len
    call write_stdout
    
    mov ecx, msg
    mov edx, msg.len
    call write_stdout
    
    mov ecx, unsavecursor
    mov edx, unsavecursor.len
    call write_stdout
    
    exit:
    mov eax, 1
    xor ebx, ebx
    int 80h
    
    ;------------------------
    write_stdout:    
    push eax
    push ebx
    mov eax, 4
    mov ebx, 1
    int 80h
    pop ebx
    pop eax
    ret
    ;---------------------
    

提交回复
热议问题