I am new to NASM and i have a project from school.
For start i need to know how to read char by char from .txt
after a long research this is my code:
section .text
global _start
_start:
;open the file
mov eax, 5 ;sys call open
mov ebx, file_name ;file name
mov ecx, 0 ;read only
mov edx, 0777 ; exe by all
int 0x80
mov [fd_in], eax
;read
loop:
mov eax, 3 ;sys call read
mov ebx, [fd_in] ;file descriptor
mov ecx, buff
mov edx, 1
int 0x80
cmp eax, 0 ;cmp EOF
je eof
;print
mov eax, 4 ;sys call write
mov ebx, 1 ;std out
mov ecx, buff
mov edx, 1
int 0x80
jmp loop
eof:
mov eax, 1
int 0x80
section .data
file_name db 'hey.txt'
section .bss
fd_in resb 1
buff resb 1
but it only print endlessly to fist char in my .txt file.
BTW in the .txt file it says "Hey my name is blah".
i would love any help or suggestions.
thanks.
Related
What command / function can I use to read the second line in the input section so the 4 and 6 I know I'll have to fix the output to output two digits. I just need helping figuring out how to read the second line of input any help would be greatly appreciated.
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
segment .data
NEWLINE db 0xa, 0xd
LENGTH equ $-NEWLINE
msg1 db "Enter a digit "
len1 equ $- msg1
msg2 db "Please enter a second digit "
len2 equ $- msg2
msg3 db "The sum is: "
len3 equ $- msg3
segment .bss
INPT resd 1
num1 resb 2
num2 resb 2
res resb 1
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num1
mov edx, 2
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, num1
mov edx, 2
int 0x80
mov eax, 0x4
mov ebx, 0x1
mov ecx, NEWLINE
mov edx, LENGTH
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num2
mov edx, 2
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, num2
mov edx, 2
int 0x80
mov eax, 0x4
mov ebx, 0x1
mov ecx, NEWLINE
mov edx, LENGTH
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0x80
; moving the first number to eax register and second number to ebx
; and subtracting ascii '0' to convert it into a decimal number
mov eax, [num1]
sub eax, '0'
mov ebx, [num2]
sub ebx, '0'
; add eax and ebx
add eax, ebx
; add '0' to to convert the sum from decimal to ASCII
add eax, '0'
; storing the sum in memory location res
mov [res], eax
; print the sum
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, res
mov edx, 1
int 0x80
exit:
mov eax, SYS_EXIT
xor ebx, ebx
int 0x80
enter image description here
I am supposed to write a small assembly "program" that simply reads a file using a loop and outputs the contents to STDOUT.
Now, I made a program that can read the file and unfortunately only outputs the first line. I also can't figure out how to use the loop.
This is my very first time working with assembly and I do it because I have to, not because I want to.
Here's my code:
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
;open the file for reading
mov eax, 5
mov ebx, file_name
mov ecx, 0 ;for read only access
mov edx, 0777 ;read, write and execute by all
int 0x80
mov [fd_in], eax
;read from file
mov eax, 3
mov ebx, [fd_in]
mov ecx, info
mov edx, 26
int 0x80
; close the file
mov eax, 6
mov ebx, [fd_in]
; print the info
mov eax, 4
mov ebx, 1
mov ecx, info
mov edx, 26
int 0x80
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
file_name db './passwd'
section .bss
fd_out resb 1
fd_in resb 1
info resb 26
I wrote this code, it reads the data from user but did not display the output. It is written in Assembly language. I am new to Assembly language. Can somebody please help me in solving this. I shall be very thankful. Thanks in advance. Here is the code:
section .data ;Data segment
userMsg db 'Please enter a number: ' ;Ask the user to enter a number
lenUserMsg equ $-userMsg ;The length of the message
dispMsg db 'You have entered: '
lenDispMsg equ $-dispMsg
section .bss ;Uninitialized data
num resb 5
section .text ;Code Segment
global _start
_start:
;User prompt
mov eax, 4
mov ebx, 1
mov ecx, userMsg
mov edx, lenUserMsg
int 80h
;Read and store the user input
mov eax, 3
mov ebx, 2
mov ecx, num
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 80h
;Output the message 'The entered number is: '
mov eax, 4
mov ebx, 1
mov ecx, dispMsg
mov edx, lenDispMsg
int 80h
;Output the number entered
mov eax, 4
mov ebx, 1
mov ecx, num
mov edx, 5
int 80h
; Exit code
mov eax, 1
mov ebx, 0
int 80h
In typical environments, file descripter 0 stands for standard input, 1 for standard output, and 2 for standard error output.
Reading from standard error output makes no sense for me.
Try changing the program for reading
;Read and store the user input
mov eax, 3
mov ebx, 2
mov ecx, num
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 80h
to
;Read and store the user input
mov eax, 3
mov ebx, 0
mov ecx, num
mov edx, 5 ;5 bytes (numeric, 1 for sign) of that information
int 80h
in order to have the system read some data from standard input.
section .data
out1: db 'Enter the number:'
out1l: equ $-out1
out2: db 'The number you entered was:'
out2l: equ $-out2
section .bss
input: resb 4
section .text
global _start
_start:
;for displaying the message
mov eax,4
mov ebx,1
mov ecx,out1
mov edx,out1l
int 80h
;for taking the input from the user
mov eax,3
mov ebx,0
mov ecx,input
mov edx,4
int 80h
;for displaying the message
mov eax,4
mov ebx,1
mov ecx,out2
mov edx,out2l
int 80h
;for displaying the input
mov eax,4
mov ebx,1
mov ecx,input
mov edx,4
int 80h
mov eax,1
mov ebx,100
int 80h
I have this piece of code in NASM (for linux) that supposed to open an existing file, read it and print the content on the screen, but does not work, can someone tell me what am I doing wrong?(hello.txt is the name of the file)
section .data
file db "./hello.txt", 0
len equ 1024
section .bss
buffer: resb 1024
section .text
global _start
_start:
mov ebx, [file] ; name of the file
mov eax, 5
mov ecx, 0
int 80h
mov eax, 3
mov ebx, eax
mov ecx, buffer
mov edx, len
int 80h
mov eax, 4
mov ebx, 1
mov ecx, buffer
mov edx, len
int 80h
mov eax, 6
int 80h
mov eax, 1
mov ebx, 0
int 80h
mov ebx, [file] ; name of the file
mov eax, 5
mov ecx, 0
int 80h
Is wrong. Loose the square brackets around file. You are passing the file name instead of a pointer to the filename.
mov ebx, file ; const char *filename
mov eax, 5
mov ecx, 0
int 80h
I see here a lot of mistakes, in order:
mov ebx, [file] ; name of the file
mov eax, 5
mov ecx, 0
int 80h
Here, as said, u must lose square brackets (because the function needs a pointer, not a value)
mov eax, 3
mov ebx, eax
mov ecx, buffer
mov edx, len
int 80h
Here, u must save the file descriptor from eax, before writing there value 3, else u just loose it
mov eax, 4
mov ebx, 1
mov ecx, buffer
mov edx, len
int 80h
Well. Here u using ebx register, so better way is to save file descriptor in memory. And for display, you take 1024 bytes from buffer, which is not correct. After reading from the file, the eax register will contain the number of characters read, so after reading from the file it will be better to store the value from the eax register in edx
mov eax, 6
int 80h
Again. U close the file, but ebx contains dirt, although it must contain a file descriptor
Correct code must look like this:
section .data
file db "text.txt",0 ;filename ends with '\0' byte
section .bss
descriptor resb 4 ;memory for storing descriptor
buffer resb 1024
len equ 1024
section .start
global _start
_start:
mov eax,5 ;open
mov ebx,file ;filename
mov ecx,0 ;read only
int 80h ;open filename for read only
mov [descriptor],eax ;storing the descriptor
mov eax,3 ;read from file
mov ebx,[descriptor] ;your file descriptor
mov ecx,buffer ;read to buffer
mov edx,len ;read len bytes
int 80h ;read len bytes to buffer from file
mov edx,eax ;storing count of readed bytes to edx
mov eax,4 ;write to file
mov ebx,1 ;terminal
mov ecx,buffer ;from buffer
int 80h ;write to terminal all readed bytes from buffer
mov eax,6 ;close file
mov ebx,[descriptor] ;your file descriptor
int 80h ;close your file
mov eax,1
mov ebx,0
int 80h
This is not a perfect code, but it should work
Currently I'm trying to loop over every single byte in a buffer (read from a file) and compare it to see if any of them is a whitespace, and write them to STDOUT. For some reason the program compiles and runs fine, but produces zero output.
section .data
bufsize dw 1024
section .bss
buf resb 1024
section .text
global _start
_start:
; open the file provided form cli in read mode
mov edi, 0
pop ebx
pop ebx
pop ebx
mov eax, 5
mov ecx, 0
int 80h
; write the contents in to the buffer 'buf'
mov eax, 3
mov ebx, eax
mov ecx, buf
mov edx, bufsize
int 80h
; write the value at buf+edi to STDOUT
mov eax, 4
mov ebx, 1
mov ecx, [buf+edi]
mov edx, 1
int 80h
; if not equal to whitespace, jump to the loop
cmp byte [buf+edi], 0x20
jne loop
loop:
; increment the loop counter
add edi, 1
mov eax, 4
mov ebx, 1
mov ecx, [buf+edi]
int 80h
; compare the value at buf+edi with the HEX for whitespace
cmp byte [buf+edi], 0x20
jne loop
; exit the program
mov eax, 1
mov ebx, 0
int 80h
The main problem was that I didn't given the address of bufsize ([bufsize]), also the loops had some problems.
Here's the fixed version, thanks everyone for your input.
section .data
bufsize dd 1024
section .bss
buf: resb 1024
section .text
global _start
_start:
; open the file provided form cli in read mode
mov edi, 0
pop ebx
pop ebx
pop ebx
mov eax, 5
mov ecx, 0
int 80h
; write the contents in to the buffer 'buf'
mov eax, 3
mov ebx, eax
mov ecx, buf
mov edx, [bufsize]
int 80h
; write the value at buf+edi to STDOUT
; if equal to whitespace, done
loop:
cmp byte [buf+edi], 0x20
je done
mov eax, 4
mov ebx, 1
lea ecx, [buf+edi]
mov edx, 1
int 80h
; increment the loop counter
add edi, 1
jmp loop
done:
; exit the program
mov eax, 1
mov ebx, 0
int 80h