This question already has answers here:
Hello, world in assembly language with Linux system calls?
(1 answer)
What happens if there is no exit system call in an assembly program?
(1 answer)
Closed 11 months ago.
This works but I don't know what the last section does. If I remove it the code segfaults, so I'm assuming it is somehow related to closing the file or handling memory allocation but as far as I am aware that what the 6 instruction is for.
section .data
msg db 'Hello, world!', 0xa
len equ $ - msg
outfile db 'test_file.txt', 0
section .text
global _start
_start:
; creating and opening
mov eax, 8 ; set instruction
mov ebx, outfile ; set file name
mov ecx, 544o ; set file permissions
int 0x80 ; make system call
; writing to file
mov ebx, eax ; move file descriptor from previous call
mov eax, 4 ; set instruction
mov ecx, msg ; set text
mov edx, len ; set number of bytes to read
int 0x80 ; make system call
; closing file
mov eax, 6 ; set instruction
int 0x80 ; make system call
; closing program?
mov eax,1
mov ebx,0
int 0x80
Related
This question already has answers here:
How do i read single character input from keyboard using nasm (assembly) under ubuntu?
(3 answers)
Reading input from keyboard with x64 linux syscalls (assembly)
(1 answer)
How to stop echo in terminal using c?
(1 answer)
Closed 2 years ago.
Is there a way to disable and enable the display of terminal input within an assembly program?
I am writing a command line program in nasm where user input it periodically read from the terminal (which I want to be displayed) and the program sleeps for certain lengths of time. During that time (unlike what I've found in the Windows command line) the user is still able to type in the terminal. I want to hide that text.
Here's a program that asks for a prompt and waits between certain message:
section .text
global _start
_start:
mov edx,lhello
mov ecx,mhello
mov ebx,1
mov eax,4 ; sys_write
int 0x80
mov dword[sleep_sec],1
mov dword[sleep_usec],0
mov ecx,0
mov ebx,sleep
mov eax,162 ; sys_nanosleep
int 0x80
mov edx,lwrite
mov ecx,mwrite
mov ebx,1
mov eax,4 ; sys_write
int 0x80
mov edx,99
mov ecx,usergift
mov ebx,0
mov eax,3 ; sys_read
int 0x80
mov edx,lsleep
mov ecx,msleep
mov ebx,1
mov eax,4 ; sys_write
int 0x80
mov dword[sleep_sec],2
mov dword[sleep_usec],0
mov ecx,0
mov ebx,sleep
mov eax,162 ; sys_nanosleep
int 0x80
mov edx,ldone
mov ecx,mdone
mov ebx,1
mov eax,4 ; sys_write
int 0x80
mov eax,1 ; sys_exit
int 0x80 ; the end!
section .bss
usergift resb 99
section .data
sleep:
sleep_sec dd 0
sleep_usec dd 0
mhello db 'hello-',0xA
lhello equ $ - mhello
mwrite db 'give me something:'
lwrite equ $ - mwrite
msleep db 'wait for 2 seconds...',0xA
lsleep equ $ - msleep
mdone db 'thank you.',0xA
ldone equ $ - mdone
gives the following output when love is inputted by the user when prompted-
hello-
give me something:love
wait for 2 seconds...
thank you.
but the user is also able to write outside of the prompt-
hello-
ha!give me something:love
wait for 2 seconds...
lies!thank you.
This question already has answers here:
Read and print user input with x86 assembly (GNU/Linux)
(2 answers)
X86 read from stdin and write to stdout without referring the standard library
(1 answer)
Linux NASM detect EOF
(1 answer)
Closed 2 years ago.
section .bss ; you can change
user_input: resb 256 ;256 byte of memory is reserved and can be accessed through the name user_input
user_input_size equ $- user_input
section .data ;you cant change
output: db "Enter your print statement: "
output_size equ $- output
section .text ;assembly code
global _start
_start:
mov eax, 4
mov ebx, 1
mov ecx, output
mov edx, output_size
int 80
mov eax, 3
mov ebx, 0
mov ecx, user_input
mov edx, user_input_size
int 80
push ecx ; not sure about this
push edx ; not sure about this
mov eax, 4
mov ebx, 0
pop ecx
pop edx
int 80
mov eax, 1
mov ebx, 0
int 80
i want to print out "Enter your print statement: ", have the user input a statement and have that inputted statement returned to the screen. The first part is working. The others not so much. When the user enters a statement is it stored eax or ecx(user_input).
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 ;
This question already has answers here:
What are file descriptors, explained in simple terms?
(13 answers)
Closed 4 years ago.
SEGMENT .data ; nothing here
SEGMENT .text ; sauce
global _start
_start:
pop ECX ; get ARGC value
mov EAX, 4 ; sys_write()
mov EBX, 1 ; /dev/stdout
;^^^^^^^^^^^
mov EDX, 1 ; a single byte
int 0x80
mov EAX, 1 ; sys_exit()
mov EBX, 0 ; return 0
int 0x80
SEGMENT .bss ; nothing here
why mov EBX, ""1"" ; /dev/stdout ?
where document I can find "1" ?
On unix systems, a process normally have 3 common I/O channels connected to it, called stdin/stdout/stderr. These have the corresponding file descriptor values 0, 1 and 2.
This is documented at: http://pubs.opengroup.org/onlinepubs/9699919799/functions/stdin.html (See also the wikipedia page )
This question already has answers here:
In NASM labels next to each other in memory are printing both strings instead of first one
(1 answer)
How does $ work in NASM, exactly?
(2 answers)
Closed 4 years ago.
running the code below generates a file with Welcome to jj Shashwat as content. what i didn't get is why does it writes Shashwat at the end of the file, Shashwat is in a totally different variable. Any idea why does this happen?
section .text
global _start ;must be declared for using gcc
_start:
;create the file
mov eax, 8
mov ebx, file_name
mov ecx, 0777 ;read, write and execute by all
int 0x80 ;call kernel
mov [fd_out], eax
; close the file
mov eax, 6
mov ebx, [fd_out]
;open the file for reading
mov eax, 5
mov ebx, file_name
mov ecx, 2 ;for read/write access
mov edx, 0777 ;read, write and execute by all
int 0x80
mov [fd_out], eax
; write into the file
mov edx,len ;number of bytes
mov ecx, msg ;message to write
mov ebx, [fd_out] ;file descriptor
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
; close the file
mov eax, 6
mov ebx, [fd_out]
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
file_name db 'myfile.txt', 0
msg db 'Welcome to jj', 0
mgafter db ' Shashwat', 0
lntwo equ $-mgafter
len equ $-msg
section .bss
fd_out resb 1
fd_in resb 1
info resb 26
That's because you said len equ $-msg after defining both msg and msgafter, so len is set to the length of both msg and msgafter, making your write call write both strings. This is because len equ $-msg means “set len to be the difference between the current location ($) and the location of msg.”
To fix this, move the len equ $-msg line right after the definition of msg.