How to call fgets in x86 assembly?

Asked
Active3 hr before
Viewed126 times

7 Answers

90%

FILE * - a FILE * to the stream to read from,int - an integer that represents the maximum number of characters to read,char * - a string that will hold the input, How can I overcome the setbacks of a negligent supervisor after earning a PhD degree?

The problem with stdin is that it's a macro that expands to something not only platform-specific, but most likely difficult to access from assembly by hand. If you're willing to sacrifice stdio and use POSIX calls instead, stdin is the same as the well-known file descriptor #0. You can therefore pass 0 to read and get almost what you were looking for. I'm pretty sure this is more assembler-friendly than the stdin C macro.

stdin

The problem with stdin is that it's a macro that expands to something not only platform-specific, but most likely difficult to access from assembly by hand. If you're willing to sacrifice stdio and use POSIX calls instead, stdin is the same as the well-known file descriptor #0. You can therefore pass 0 to read and get almost what you were looking for. I'm pretty sure this is more assembler-friendly than the stdin C macro.

stdio

The problem with stdin is that it's a macro that expands to something not only platform-specific, but most likely difficult to access from assembly by hand. If you're willing to sacrifice stdio and use POSIX calls instead, stdin is the same as the well-known file descriptor #0. You can therefore pass 0 to read and get almost what you were looking for. I'm pretty sure this is more assembler-friendly than the stdin C macro.

stdin

The problem with stdin is that it's a macro that expands to something not only platform-specific, but most likely difficult to access from assembly by hand. If you're willing to sacrifice stdio and use POSIX calls instead, stdin is the same as the well-known file descriptor #0. You can therefore pass 0 to read and get almost what you were looking for. I'm pretty sure this is more assembler-friendly than the stdin C macro.

0

The problem with stdin is that it's a macro that expands to something not only platform-specific, but most likely difficult to access from assembly by hand. If you're willing to sacrifice stdio and use POSIX calls instead, stdin is the same as the well-known file descriptor #0. You can therefore pass 0 to read and get almost what you were looking for. I'm pretty sure this is more assembler-friendly than the stdin C macro.

stdin
load more v
88%

In assembly, there is one weird annoyance with printf, that it requires a "16-byte aligned stack". 


puts("Sup?");
load more v
72%

Software Development Forum , Programming Forum , Learning assembly 7 , Is assembly a good starter language? 12

Anyway, here is my test code. It builds and runs correctly under Cygwin.

;
test_io.asm;
Test _read and _write system calls
;;
nasm - o test_io.o - fwin32 test_io.asm;
gcc - o test_io test_io.o
section.data
prompt db 'Enter a string: ', 0
prompt_len equ $ - prompt

BUFSZ equ 1024
STDIN equ 0
STDOUT equ 1

section.bss
buffer resb BUFSZ

section.text
global _main
extern _read, _write
_main:
   push prompt_len
push prompt
push STDOUT
call _write
add esp, 12

push BUFSZ - 1;
leave space
for terminating 0
push buffer
push STDIN
call _read
add esp, 12

push eax;
_read returns string length in eax
push buffer
push STDOUT
call _write
add esp, 12

mov eax, 0
ret
65%

In the previous post, we reviewed the very basic of x86 assembly language. Let’s do some exercise. The main objective here is to do decompiling. Because in real-world security tasks, we don’t get to see the source code to find the vulnerabilities. More often, we have access to the binary executable file, and we can use GDB debugger to look at the machine instructions in assembly. Therefore, decompiling is an important skill.,In order to provide the correct input, we need to decompile the algorithm used in wiring(), fun() and bliss().,As we can see, the wiring() is hidden to us. We want to use GDB debugger to look at its assembly code:,I don’t think I can write a clear explanation here in this post (maybe better with a video). So I will just skip the analysis and decompiling process. My solution is provided below.

 

As we can see, the wiring() is hidden to us. We want to use GDB debugger to look at its assembly code:

wiring()
load more v
75%

<open file>
   while(fgets(<char array>, <size of array>, <file pointer>) != null)
40%

Ok, what do we see here? This simple code represents Hello world assembly program for the Linux x86_64 architecture. We can see two sections here:,The Linux kernel provides a set of these functions and each architecture provides its own set. For example: the x86_64 provides 322 system calls and the x86 provides 358 different system calls. Ok, a system call is just a function. Let's look on a simple Hello world example that's written in the assembly programming language:, How the Linux kernel runs a program , How the Linux kernel handles a system call

The Linux kernel provides a set of these functions and each architecture provides its own set. For example: the x86_64 provides 322 system calls and the x86 provides 358 different system calls. Ok, a system call is just a function. Let's look on a simple Hello world example that's written in the assembly programming language:

.data

msg:
   .ascii "Hello, world!\n"
len = . - msg

   .text
   .global _start

_start:
   movq $1, % rax
movq $1, % rdi
movq $msg, % rsi
movq $len, % rdx
syscall

movq $60, % rax
xorq % rdi, % rdi
syscall
load more v
22%

Function Calls in x86 Assembly,x86 calling conventions,We can also chain filters:,0816243240485664bufferrbpreta(pop)(ret)…garbage0x4008830x6010600x400810

void pwnme() {
   char buf[32];
   memset(buf, 0, 32);
   puts("For my first trick, I will attempt to fit 50 bytes of user input "
      "into 32 bytes of stack buffer;\nWhat could possibly go wrong?");
   puts("You there madam, may I have your input please? And don't worry "
      "about null bytes, we're using fgets!\n");
   printf(">");
   fgets(buf, 50, stdin);
}
load more v

Other "undefined-undefined" queries related to "How to call fgets in x86 assembly?"