LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   [asm] Status-Byte (printer) (http://www.linuxquestions.org/questions/programming-9/%5Basm%5D-status-byte-printer-109446/)

jb_li 10-28-2003 07:59 AM

[asm] Status-Byte (printer)
 
Hi,

First of all i have to say that I am very new to the Assembler programming language.
I tried the following on a linux ix86 box without success.

Code:

section .text
global _start

msg    db "Druckertest ...",0x0A ;0x0A->zeilenumbruch
msg2    db "Drucker bereit",0x0A
len    equ $-msg
len2    equ $-msg2

_start:
        mov eax,4 ; Ausgabe von msg
        mov ebx,1     
        mov ecx,msg
        mov edx,len
        int 0x80
        jmp Drucker_test


Drucker_test:
        mov ah,02
        xor dx,dx
        int 17h
        test ah,10000000b
        je Bereit

Bereit:
        mov eax, 4
        mov ebx, 1
        mov ecx, msg2
        mov edx, len2
        int 0x80
        jmp Endeprg

Endeprg:
        mov eax,1      ;syscall 1 = exit
        int 0x80        ;exit

My goal is to fetch the printers Statusbyte to analyze how the printer works, but when the program reaches "int 17h" it runs into a segmentation fault.
Does anyone has any idea how to solve this problem?

Thanks in advance,
h.d.

LogicG8 10-28-2003 10:18 AM

I have never programmed in assembly on Linux and
it has been a while since I've touched assembly at all
but off the top of my head I see:

1) This might be it, I think you might not be able
to access the hardware directly like that from
user space. Try running the program as root.
I doubt that will work. You will probably have
to use system calls to get the information
indirectly.

2) This just kind of jumped out at me but won't solve
your problem you jump unecessarily.

int 0x80
jmp Drucker_test
Drucker_test:

could just be
int 0x80
Drucker_test:
...

And the same for the other times you do it.

and when you
je Bereit
Bereit:
There is no point, you will always fall through
to Bereit

Hope this helps

jb_li 10-28-2003 10:59 AM

Hi,

you are right, the jumps are unecessary. I thought I can prevent some problems if the program becomes more complex. :-) But that shouldnt be the reason for the segmentation fault when calling int 17h.
You said accessing the hardware directly from user space might not work. Do I have the opportunity to implement these lines into a LKM? Or which syscall do I need to access the hardware to get the printers status byte?

Thanks in advance
h.d.

LogicG8 10-28-2003 11:43 AM

You could do that from kernel module but I don't know if you'd
have a conflict form the normal parallel port driver. But as we've
just left user space and entered the kernel I am way out of my
league. It would be a terrible security risk to be able to have
direct hardware access from userspace. If you can find out what
int 0x17 does exactly you might be able to stay in nice friendly
userspace by manipulating the i/o lines. See the i/o port programming
how-to tldp.

infamous41md 10-28-2003 12:01 PM

i think the problem is the same one encountered often in shellcode. it has to do with
the registers being clean when making interrupts, here:
Code:

Drucker_test:
        mov ah,02
        xor dx,dx
        int 17h

you move 02 into ah, but the other 3 bytes of EAX probably contain junk. see if
movl eax, 02 works. that might be the cause.

LogicG8 10-28-2003 12:50 PM

Interupts don't work from userspace.

but you're right problems are often caused
by dirty registers. However he moves 2 to ah
not al so it should be
mov_l eax, 0x200

infamous41md 10-28-2003 01:24 PM

int = interrupt, right? you interrupt the kernel to make the system call on your behalf i thought.
yep, my bad with ah/al i havent worked in asm in a bit.

edit: yep after reading intel manual int i was correct, int:
"...int genreates a call to the interrupt or exception handler specified by arggument"
we prolly jus got messed up on definitions?

LogicG8 10-28-2003 01:53 PM

Yeah,
I should be more clear.
int 0x80 is allowed and you are correct that is how you
interact with the kernel for system calls. Other interrupts
aren't allowed. int is a privileged instruction. Using int in
usermode generates an exception which is why his program
segfaults. The kernel masks int 0x80 and catches the exception
it when a process uses it. The kernel does this sort of thing for
other stuff too. For example when you want to use floating
point instructions on an i386 w/ no fpu. The kernel will catch
the "illegal" instruction exception and instead of giving your
program a SIGILL it will emulate the instruction and hand it
back to the program transparently.

infamous41md 10-28-2003 02:55 PM

ahhh now i gotcha. thanks for the xtra info as well i wasn't fully aware that's how int 0x80 was handled. :)

jb_li 10-28-2003 03:59 PM

Ok, thank you all. I think, now I understand why it doesnt work to call the interrupt, but another question regarding that topic ... What is the way a driver (module in the linux world) initialize the printer to communicate with the system. Interrupt 17h, function 1 (ah =1) specifies a printer initialization. What does that mean in clear text? Does that has anything to do with driver wrtiting or is it just another method to confuse me. :-)

Thanks,
h.d.


All times are GMT -5. The time now is 08:39 AM.