nasm - How to take string input from file in assembly Language? -
i have file named customer.txt.
customer.txt
amin jamal i need sample code takes string input(amin) customer.txt file , print using c printf function.
my code
section .bss s: resb 100 section .data fmt: db "%s",10,0 name: db "customer.txt",0 mode: db "r+",0 fp: dq 0 section .text global main extern fopen extern fgets extern printf main: push rbp mov rdi , name; mov rsi , mode call fopen mov [fp] , rax mov rdi , s mov rsi , 7 mov rdx , [fp] call fgets mov rdi , fmt mov rsi , s call printf pop rbp ret this can not take full string.
i using nasm assembler. os 64bit linux.
i use nasm command assemble, compile , run.
nasm -f elf64 file.asm gcc file.o ./a.out
your code works fine me. can add error handling. tried code , worked fine me.
section .bss s : resb 100 section .data fmt : db "%s",10,0 name : db "customer.txt",0 mode : db "r+",0 err1 : db "failed open file", 10, 0 err2 : db "failed read file", 10, 0 section .text extern fopen extern fgets extern printf global main main: push rbp ; open file reading mov rdi , name mov rsi , mode call fopen cmp rax, 0 ; check errors je .errfopen ; read first 7 characters open file mov rdi , s mov rsi , 7 mov rdx , rax ; file pointer call fgets cmp rax, 0 ; check errors je .errfgets mov rdi, fmt mov rsi, s call printf pop rbp ret .errfopen: mov rdi, err1 call printf pop rbp ret .errfgets: mov rdi, err2 call printf pop rbp ret
Comments
Post a Comment