Skip to content
Advertisement

How to print the content of a text file to STDOUT in Linux Assembly Language using c library functions?

First I tried opening a file with fopen function and print content of the file using fprint function but it was just printing a bunch of symbols to the terminal.

After a while I realized that it does not take pointer to a stream as argument and above mentioned behaviour was expected. It was printing the actual pointer value. putc or puts function also does not seem to take a pointer to the file I/O stream as an argument.

My best guess right now is I have to access the buffer fopen function created somehow! But I have no idea how or if it is possible. To sum it all up I am absolutely stuck right now.

The ultimate goal here is to get input from STDIN and/or a file do some processing on the text(eg: lowercase to uppercase) and output the result to STDOUT and/or a file. I figured If I am able to get the answer to above mentioned problem then It should help with the ultimate goal maybe.

PS: Let me know If the question can be improved in any way. Thank you.

Maybe the following code will help to get a better understanding of what I am trying to say.

.section .data
o_rdonly:
    .ascii "r"
content:
    .ascii "Content of the file: %dn"  #This is how I figured what those random symbols were.

.section .bss
.lcomm buffer, 10

.section .text
.globl _start
_start:
    movl %esp, %ebp

    pushl $o_rdonly
    pushl 8(%ebp)
    call fopen

    ##Print the content of the file?##
    #pushl %eax #
    #call printf    #It just prints the actual address of the File I/O pointer.

    pushl $0
    call exit

Advertisement

Answer

You need to first read from the file using fread or fgets or some other function and then write what you read to standard output. There is no shortcut to directly print the contents of one file.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement