I was wondering something, I know that on windows you can't interact directly with the hardware, you have to use the system API. Now in Linux in the other hand, it looks to me if this can actually be done, or am I wrong?
The thing reason why I say this is in the example below:

section .text
    global _start   
_start:             
    mov edx,len     
    mov ecx,msg     
    mov ebx,1       
    mov eax,4       
    int 0x80        

    mov eax,1       
    int 0x80        

section .data
msg db 'Hello, world!', 0xa  
len equ $ - msg     

I don't see any system API calls, where as in windows:

global _start

extern _GetStdHandle@4
extern _WriteConsoleA@20
extern _ExitProcess@4

section .data
        str:     db 'hello, world',0xA
        strLen:  equ $-str

section .bss
        numCharsWritten:        resb 1

section .text
        _start:

        ;
        ; HANDLE WINAPI GetStdHandle( _In_  DWORD nStdHandle ) ;
        ;
        push    dword -11       ; Arg1: request handle for standard output
        call    _GetStdHandle@4 ; Result: in eax

        ;
        ; BOOL WINAPI WriteConsole(
        ;       _In_        HANDLE hConsoleOutput,
        ;       _In_        const VOID *lpBuffer,
        ;       _In_        DWORD nNumberOfCharsToWrite,
        ;       _Out_       LPDWORD lpNumberOfCharsWritten,
        ;       _Reserved_  LPVOID lpReserved ) ;
        ;
        push    dword 0         ; Arg5: Unused so just use zero
        push    numCharsWritten ; Arg4: push pointer to numCharsWritten
        push    dword strLen    ; Arg3: push length of output string
        push    str             ; Arg2: push pointer to output string
        push    eax             ; Arg1: push handle returned from _GetStdHandle
        call    _WriteConsoleA@20


        ;
        ; VOID WINAPI ExitProcess( _In_  UINT uExitCode ) ;
        ;
        push    dword 0         ; Arg1: push exit code
        call    _ExitProcess@4

Here you have to use windows API calls to write to the screen.

So does linux allow you to do direct hardware interaction?

Recommended Answers

All 2 Replies

In your first example, your using an interrupt to make a system call. If you want, you can think of system calls (and more generally, interupts) as a kind of primitive api. Your not 'directly' interacting with the hardware.

You can also make calls to libraries in linux, much the same as windows.

On to the subject of interracting with hardware. You actually can directly interract with it in Windows (or, as close as whats reasonable). It's called writting a driver.

Linux has another, easier method of directly interacting with hardware. Most hardware is mapped to a file in /dev/ . The idea is that writting to a file, writes to that device, and reading from that file, reads from the device.

There's even a file that is mapped to your ram. There's another file thats mapped to your speakers. I used to find it fun to read my ram into my speakers.

P.S. In general, you want to avoid using hardware directly, unless you have a good reason. You should probably stick with using library API's, or syscalls.

No multitasking operating system will let you directly manipulate the hardware, without the OS being involved somewhere along the way. If you had one program trying to write something directly to the screen, and another program trying to write to that selfsame area of the screen, at the same time, chaos would ensue. Similarly, the OS system will prevent scribbling all over another program's RAM, or, even worse, overwriting another program's files.

A multi tasking operating system is there to operate as a benevolent dictator, and the hardware is designed in such a way as to facilitate that role. There are some things the hardware will let only the OS do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.