Create/Write to file x86 Linux - linux

I have been trying to teach myself some 32-bit x86 (NASM). I am trying to have a user input a file name, open/create the file, then take a user's message and write that message to the file. I have gone through it in GDB and all syscalls returned correctly. After the program runs the file appears to be created improperly and nothing is written to it. I ahev seen some of the other question that are similar but my code seems to be nearly the same as their's so I can't seem to figure out what the heck is going on.
Here is my noob code:
global _start
section .data
fmsg: db "Enter Filename: ", 0
.len: equ $ - fmsg
umsg: db "Enter message: ", 0
.len: equ $ - umsg
buff: times 50 db 0 ;array for user string
.blen: equ $ - buff
fname: times 50 db 0 ;array for filename
.flen: equ $ - fname
;modes
O_RDONLY: db 0 ;read-only
O_WRONLY: db 1 ;wirte-only
O_RDWR: db 2 ;read and write
;flags
O_CREAT: dw 100o ;create file if file doesnt exists
O_TRUNC: dw 1000o ;truncate file
O_APPEND: dw 2000o ;append to file
section .bss
fd: resd 1 ;file descriptor
bret: resd 1 ;read buffer return value
fret: resd 1 ;read filename return value
tmp: resd 1 ;temp 4 byte variable
section .text
_start:
fprompt: ;Print prompt
mov eax, 0x4 ;syscall 4 - write()
mov ebx, 0x1 ;file desc 1 - stdout
mov ecx, fmsg ;print message
mov edx, fmsg.len ;length of message
int 80h ;syscall interupt
filein:
mov eax, 0x3 ;syscall 3 - read()
mov ebx, 0x0 ;file desc 0 - stdin
mov ecx, fname ;dst buffer
mov edx, fname.flen ;length of buffer
int 80h ;syscall interupt
mov [fret], eax ;save return value to file return variable
cmp eax, edx ;read max bytes or more?
jb fileopen ;jmp is bytes read < max
mov bl, [ecx+eax-1] ;grab last byte # last index before '\0'
cmp bl, 10 ;does it = '\n' ?
je clean1
inc DWORD [fret] ;len++
clean1: ;loop to clear excess input, if any
mov eax, 0x3 ;syscall 3 - read()
mov ebx, 0x0 ;file desc 0 - stdin
mov ecx, tmp ;temp buffer
mov edx, 0x1 ;read only 1 byte
int 80h ;;syscall interupt
test eax, eax ;EOF?
jz fileopen ;Yes, jump to pback
mov al, [tmp] ;put character into lower 8 bits of EAX
cmp al, 10 ;is it = to lf ?
jne clean1 ;no, jump to begining of loop
fileopen:
mov eax, 0x05
mov ebx, fname ;filename
or ecx, O_CREAT ;if it doesn't exist create the file
or ecx, O_TRUNC ;truncate
mov edx, O_WRONLY ;write only
int 80h ;syscall interupt
mov [fd], eax ;save file descripor
prompt2:
mov eax, 0x4 ;syscall 4 - write()
mov ebx, 0x1 ;file desc 1 - stdout
mov ecx, umsg ;print message
mov edx, umsg.len ;length of message
int 80h
userin:
mov eax, 0x3 ;syscall 3 - read()
mov ebx, 0x0 ;file desc 0 - stdin
mov ecx, buff ;dst buffer
mov edx, buff.blen ;length of buffer
int 80h ;syscall interupt
mov [bret], eax ;save return value to buff return variable
cmp eax, edx ;read max bytes or more?
jb writetofile ;jmp is bytes read < max
mov bl, [ecx+eax-1] ;grab last byte # last index before '\0'
cmp bl, 10 ;does it = '\n' ?
je clean2
inc DWORD [bret] ;len++
clean2: ;loop to clear excess input, if any
mov eax, 0x3 ;syscall 3 - read()
mov ebx, 0x0 ;file desc 0 - stdin
mov ecx, tmp ;temp buffer
mov edx, 0x1 ;read only 1 byte
int 80h ;syscall
test eax, eax ;EOF?
jz writetofile ;Yes, jump to pback
mov al, [tmp] ;put character into lower 8 bits of EAX
cmp al, 10 ;is it = to lf ?
jne clean2 ;no, jump to begining of loop
writetofile:
mov eax, 0x4 ;syscall 4 - write()
mov ebx, [fd] ;file desc 1 - stdout
mov ecx, buff ;print message
mov edx, [bret] ;length of message
int 80h ;syscall interupt
closefile:
mov eax, 0x6 ;syscall 6 - close()
mov ebx, [fd] ;file desc
int 80h ;syscall interupt
exit: ;return 0
mov eax, 1 ;syscall 1 - exit()
mov ebx, 0 ;return val
int 80h ;syscall interupt
Here is an example of what I get after running it:
The file "test.txt?" shows up and also shows up as an executable even though I set only read/write for the file. Even when I try to open it there is nothing there. Any thoughts? Also As I mentioned, i am new and teaching myself so if you have any good tips on improvement for other areas of the program please let me know! :)

We have multiple errors (or one big one) in the following three lines of code:
or ecx, O_CREAT ;if it doesn't exist create the file
or ecx, O_TRUNC ;truncate
mov edx, O_WRONLY ;write only
The problem:
What values do the registers ecx and edx have after these lines?
You perform two or operations with the ecx register but obviously it is not initialized at that moment!
This means that you can be sure that the bits representing O_CREAT and O_TRUNC (whatever these values mean - see below) are set but you don't know which values the other bits have.
The O_WRONLY bit should be set in ecx, not in edx. edx should contain the desired file mode instead.
Unfortunately there are two different types of assembler - I don't know which type NASM is of:
One type of assembler would interpret the first instruction as: or ecx, [O_CREAT]
The other type would interpret it as: or ecx, address_of(O_CREAT)
In the first case the instruction mov edx, O_WRONLY will read four bytes starting with the O_WRONLY byte into the edx register so edx will have the value 0x400201 (O_CREAT*0x10000+O_RDWR*0x100+O_WRONLY).
In the second case edx will contain the address of O_WRONLY rather than the value.
The value will be wrong in any case.

Storing your mode constants in memory is really inefficient, even if you were doing it right (which you aren't).
You could trace what system calls you're actually making by running strace ./writefile.
You use O_RDONLY: db 0 to store a byte of static data (in the read/write section instead of .rodata for some reason). Instead, you should define assembler constants with equ:
O_RDONLY equ 0
O_WRONLY equ 1
O_RDWR equ 2
O_CREAT equ 100o ;create file if file doesnt exists
O_TRUNC equ 1000o ;truncate file
O_APPEND equ 2000o ;append to file
Then you can write
mov ecx, O_CREAT | O_TRUNC | O_WRONLY
mov edx, 0777o ; mode is the permission bits if open() creates the file
See the open(2) man page to learn how its args work.
The assembler will do the OR for you at assemble time, instead of having the CPU do 2 loads from memory.
What you actually wrote:
or ecx, O_CREAT ;if it doesn't exist create the file
or ecx, O_TRUNC ;truncate
assembles to two or ecx, imm32 instructions, which OR the addresses of the two dw locations into the original value of ecx. If you had written
movzx ecx, word [O_CREAT]
or cx, [O_TRUNC]
or cx, [O_WRONLY]
your code would have worked, but that would be really silly. (And on some CPUs cause a partial-register stall when something reads the full ecx after you wrote only the low 16 bits.)
If you'd written or ecx, [O_TRUNC], it would do a 32-bit load, so you'd effectively be doing ecx |= (2000o << 16) | 1000o. i.e. the word stored at O_APPEND would be ORed into the high 16 bits of ECX, where it may have a different meaning.
Similarly, mov edx, O_WRONLY assembles to mov edx, imm32, where the address is the immediate. This is why you ended up with weird garbage for the file mode (including the sticky bit set).
Use a debugger.

Related

To display characters in reverse order using nasm [infinite loop running]

THE PROGRAM IS USED TO ACCEPT CHARACTERS AND DISPLAY THEM IN REVERSE ORDER
The code is included here:
section .bss
num resb 1
section .text
global _start
_start:
call inputkey
call outputkey
;Output the number entered
mov eax, 1
mov ebx, 0
int 80h
inputkey:
;Read and store the user input
mov eax, 3
mov ebx, 2
mov ecx, num
mov edx, 1
int 80h
cmp ecx, 1Ch
je .sub2
push ecx
jmp inputkey
.sub2:
push ecx
ret
outputkey:
pop ecx
;Output the message
mov eax, 4
mov ebx, 1
;mov ecx, num
mov edx, 1
int 80h
cmp ecx, 1Ch
je .sub1
jmp outputkey
.sub1:
ret
The code to compile and run the program
logic.asm
is given here:
nasm -f elf logic.asm
ld -m elf_i386 -s -o logic logic.o
./logic
There are a few problems with the code. Firstly, for the sys_read syscall (eax = 3) you supplied 2 as the file descriptor, however 2 refers to stderr, but in this case you'd want stdin, which is 0 (I like to remember it as the non-zero numbers 1 and 2 being the output).
Next, an important thing to realize about the ret instruction is that it pops the value off the top of the stack and returns to it (treating it as an address). Meaning that even if you got to the .sub2 label, you'd likely get a segfault. With this in mind, the stack also tends to not be permanent storage, as in it is not preserved throughout procedures, so I'd recommend just making your buffer larger to e.g. 256 bytes and increment a value to point to an index in the buffer. (Using a fixed-size buffer will keep you from getting into the complications of memory allocation early, though if you want to go down that route you could do an external malloc call or just an mmap syscall.)
To demonstrate what I mean by an index into the reserved buffer:
section .bss
buf resb 256
; ...
inputkey:
xor esi, esi ; clear esi register, we'll use it as the index
mov eax, 3
mov ebx, 0 ; stdin file descriptor
mov edx, 1 ; read one byte
.l1: ; loop can start here instead of earlier, since the values eax, ebx and edx remain unchanged
lea ecx, [buf+esi] ; load the address of buf + esi
int 80h
cmp [buf+esi], 0x0a ; check for a \n character, meaning the user hit enter
je .e1
inc esi
jmp .l1
.e1:
ret
In this case, we also get to preserve esi up until the output, meaning that to reverse the input, we just print in descending order.
outputkey:
mov eax, 4
mov ebx, 1 ; stdout
mov edx, 1
.l2:
lea ecx, [buf+esi]
int 80h
test esi, esi ; if esi is zero it will set the ZF flag
jz .e2:
jmp .l2
.e2:
ret
Note: I haven't tested this code, so if there are any issues with it let me know.

Converting user input to all caps in assembly (NASM) [duplicate]

This question already has answers here:
X86 NASM Assembly converting lower to upper and upper to lowercase characters
(5 answers)
X86 Assembly Converting lower-case to uppercase
(1 answer)
Closed 3 years ago.
I want to change the string to all caps, although I am having trouble getting the length of the input. What i have tried so far is moving the address of the message into a registrar then indexing through the string and also increment a counter variable. Then comparing the char in the address to a '.' (signifying the end of the message) and if its found not to be equal it will recall this block of statements. At least this is what I want my code to do. Not sure if this is even the right logic. I know there are alot of errors and its messy but I'm learning so please just focus on my main question. thank you! EDIT: the input i use is 'this is a TEST.'
;nasm 2.11.08
SYS_Write equ 4
SYS_Read equ 3
STDIN equ 0
STDOUT equ 1
section .bss
message resb 15
counter resb 2
section .data
msg1: db 'Enter input (with a period) that I will turn into all capitals!',0xa ;msg for input
len1 equ $- msg1
section .text
global _start
_start:
mov eax, SYS_Write ; The system call for write (sys_write)
mov ebx, STDOUT ; File descriptor 1 - standard output
mov ecx, msg1 ; msg to print
mov edx, len1 ; len of message
int 0x80 ; Call the kernel
mov eax, SYS_Read ;system call to read input
mov ebx, STDIN ;file descriptor
mov ecx, message ;variable for input
mov edx, 15 ;size of message
int 0x80 ;kernel call
mov [counter], byte '0'
getLen:
mov eax, message
add eax, [counter]
inc byte [counter]
cmp eax, '.'
jne getLen
mov eax, SYS_Write ; this is to print the counter to make sure it got the right len
mov ebx, STDOUT
mov ecx, counter
mov edx, 2
int 0x80
jmp end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mov eax, [message]
;add eax, counter
cmp eax, 90
jg toUpper
toUpper:
sub eax, 32
mov [message], eax
mov eax, SYS_Write ; The system call for write (sys_write)
mov ebx, STDOUT ; File descriptor 1 - standard output
mov ecx, message ; Put the offset of hello in ecx
mov edx, 10 ; helloLen is a constant, so we don't need to say
int 0x80 ; Call the kernel
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
end:
mov eax,1 ; The system call for exit (sys_exit)
mov ebx,0 ; Exit with return code of 0 (no error)
int 0x80 ;

How to read and display the contents of a text file in nasm?

I want to read and display the contents of a text file using nasm and Linux system calls. My text file is named "new.txt". I wrote the following code and am receiving no output on the terminal.
section .data
line db "This is George,",
db " and his first line.", 0xa, 0
len equ $ - line
line2 db "This is line number 2.", 0xa, 0
len2 equ $ - line2
filename: db 'ThisIsATestFile.txt', 0
section .bss
bssbuf: resb len ;any int will do here, even 0,
file: resb 4 ;since pointer is allocated anyway
global _start
section .text
_start:
; open file in read-only mode
mov eax, 5 ;sys_open file with fd in ebx
mov ebx, filename ;file to be opened
mov ecx, 0 ;O_RDONLY
int 80h
cmp eax, 0 ;check if fd in eax > 0 (ok)
jbe error ;can not open file
mov ebx, eax ;store new (!) fd of the same file
; read from file into bss data buffer
mov eax, 3 ;sys_read
mov ecx, bssbuf ;pointer to destination buffer
mov edx, len ;length of data to be read
int 80h
js error ;file is open but cannot be read
cmp eax, len ;check number of bytes read
jb close ;must close file first
; write bss data buffer to stderr
mov eax, 4 ;sys_write
push ebx ;save fd on stack for sys_close
mov ebx, 2 ;fd of stderr which is unbuffered
mov ecx, bssbuf ;pointer to buffer with data
mov edx, len ;length of data to be written
int 80h
pop ebx ;restore fd in ebx from stack
close:
mov eax, 6 ;sys_close file
int 80h
mov eax, 1 ;sys_exit
mov ebx, 0 ;ok
int 80h
error:
mov ebx, eax ;exit code
mov eax, 1 ;sys_exit
int 80h
When you write
jb close ;must close file first
you, in fact, are jumping to close, not calling it.
Once you reach close, you just exit after you close the file:
close:
mov eax, 6 ;sys_close file
int 80h
mov eax, 1 ;sys_exit
mov ebx, 0 ;ok
int 80h
Perhaps you want to either call close in some way (and then ret from close) or jump back to where you were to continue what you were doing?
Also, keep in mind that jbe is an unsigned comparison, so when you write:
cmp eax, 0 ;check if fd in eax > 0 (ok)
jbe error ;can not open file
You in fact will not detect negative numbers. Consider jle instead. (You can consult this handy resource about jumps to use.)

NASM, read a file and print the content

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

NASM Linux Assembly Printing Integers

I am trying to print a single digit integer in nasm assembly on linux. What I currently have compiles fine, but nothing is being written to the screen. Can anyone explain to me what I am doing wrong here?
section .text
global _start
_start:
mov ecx, 1 ; stores 1 in rcx
add edx, ecx ; stores ecx in edx
add edx, 30h ; gets the ascii value in edx
mov ecx, edx ; ascii value is now in ecx
jmp write ; jumps to write
write:
mov eax, ecx ; moves ecx to eax for writing
mov eax, 4 ; sys call for write
mov ebx, 1 ; stdout
int 80h ; call kernel
mov eax,1 ; system exit
mov ebx,0 ; exit 0
int 80h ; call the kernel again
This is adding, not storing:
add edx, ecx ; stores ecx in edx
This copies ecx to eax and then overwrites it with 4:
mov eax, ecx ; moves ecx to eax for writing
mov eax, 4 ; sys call for write
EDIT:
For a 'write' system call:
eax = 4
ebx = file descriptor (1 = screen)
ecx = address of string
edx = length of string
After reviewing the other two answers this is what I finally came up with.
sys_exit equ 1
sys_write equ 4
stdout equ 1
section .bss
outputBuffer resb 4
section .text
global _start
_start:
mov ecx, 1 ; Number 1
add ecx, 0x30 ; Add 30 hex for ascii
mov [outputBuffer], ecx ; Save number in buffer
mov ecx, outputBuffer ; Store address of outputBuffer in ecx
mov eax, sys_write ; sys_write
mov ebx, stdout ; to STDOUT
mov edx, 1 ; length = one byte
int 0x80 ; Call the kernel
mov eax, sys_exit ; system exit
mov ebx, 0 ; exit 0
int 0x80 ; call the kernel again
From man 2 write
ssize_t write(int fd, const void *buf, size_t count);
In addition to the other errors that have been pointed out, write() takes a pointer to the data and a length, not an actual byte itself in a register as you are trying to provide.
So you will have to store your data from a register to memory and use that address (or if it's constant as it currently is, don't load the data into a register but load its address instead).

Resources