How to display date using x86 NASM? - linux

Trying to display the date and it tells me I need a coma after the operand in line 16
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov edx, len ;message length
mov ecx, msg ;message to write
mov ebx, 1 ;file descriptor (stdout)
mov eax, 4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax, 1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!',0xa ;our dear string
len equ $ - msg ;length of our dear string
dt db __DATE__ ,0xa
len2 equ $ - dt

Related

Appending Two Strings in Assembly

We've learned only a touch (by touch I mean nothing in class, only theoretical talk of how registers work) of assembly in school and my professor wants us to do something in assembly a little harder than adding two integers. I've been doing a bunch of research and have come up with some code. I am using Microsoft Visual Code on a Linux VM. From my understanding, the code puts input1 into ECX, then puts input2 into ECX, then this essentially appends IF the length of input1 is at least 10 bytes. Not entirely sure how this is working. If I use less than 10 bytes for the first input I get the next input printed on a new line.
I've been trying to understand how [variable] and variable, value vs. address if I understand correctly, fit into this and have played around with reserved byte variables. I'm not asking for my project to be done, but I feel like I'm blindly trying this as opposed to without direction. Thanks in advance guys!
section .text
global main ;must be declared for using gcc
extern printf ;C library used to print
extern exit ;C library used to exit
extern scanf ;C library used to input user entry
main: ;tell linker entry point, called main due to C library usage
;Reads prompt message
mov eax, 4 ;system call number (sys_write)
mov ebx, 1 ;file descriptor (stdout)
mov ecx, prompt1 ;message to write called prompt
mov edx, len1 ;message length
int 0x80 ;call kernel
;Accept user input
mov eax, 3 ;system call number (sys_read)
mov ebx, 0 ;file descriptor (stdin)
mov ecx, input1 ;variable to read into, i.e. input
mov edx, 25 ;input length
int 0x80 ;call kernel
;Reads prompt message
mov eax, 4 ;system call number (sys_write)
mov ebx, 1 ;file descriptor (stdout)
mov ecx, prompt2 ;message to write called prompt
mov edx, len2 ;message length
int 0x80 ;call kernel
;Accept user input
mov eax, 3 ;system call number (sys_read)
mov ebx, 0 ;file descriptor (stdin)
mov ecx, input2 ;variable to read into, i.e. input
mov edx, 25 ;input length
int 0x80 ;call kernel
;cld
mov al, 0
mov ecx, 50
mov edi, input3
repne scasb
dec edi
mov ecx, 2
mov esi, input4
rep movsb
int 0x80
;Display user input
mov eax, 4 ;system call number (sys_write)
mov ebx, 1 ;file descriptor (stdout)
mov ecx, input1 ;message to write
mov edx, 50 ;message length
int 0x80 ;call kernel
call exit
section .data
prompt1 db 'Please enter your first string: ', 0xa ;string to print for user input
len1 equ $ - prompt1 ;length of string prompt
prompt2 db 'Please enter your second string: ',0xa ;string to print for user input
len2 equ $ - prompt2 ;length of string prompt
input3: times 10 db 0 ;variable input is of 10 bytes, null string terminated
input4: times 1 db 0
section .bss
input1 resb 50
input2 resb 25

Invalid combination of opcode and operands in program assembly

At start I want to say that I'm a beginner in Assembly. I want to write a program which firstly adds two numbers and then divide the result by 2, so i want to get an average of two numbers. The problem is in section after dividing, but program without this section about dividing works well and prints the sum.
section .data
mess1 db 'Podaj pierwsza liczbe: '
len1 equ $- mess1
mess2 db 'Podaj druga liczbe: '
len2 equ $- mess2
mess3 db 'Wynik: '
len3 equ $- mess3
section .bss
zmienna1 resb 4
zmienna2 resb 4
wynik resb 8 ;result
section .text
global _start
_start:
mov eax,4
mov ebx,1
mov ecx,mess1
mov edx,len1
int 0x80
mov eax,3 ;sys_read to 3
mov ebx,0 ;stdin
mov ecx,zmienna1
mov edx,4 ;4 to rozmiar
int 0x80
mov eax,4
mov ebx,1
mov ecx,mess2
mov edx,len2
int 0x80
mov eax,3
mov ebx,0 ;sys_read i stdin
mov ecx,zmienna2
mov edx,4 ;4 rozmiar
int 0x80
mov eax,4
mov ebx,1
mov ecx,mess3
mov edx,len3
int 0x80
;Teraz wrzuc zmienna1 do eax, a zmienna2 do ebx
; odejmij ASCII-owe '0' aby przekonwertowac na dzisiejtne
mov eax,[zmienna1]
sub eax,'0'
mov ebx,[zmienna2]
sub ebx,'0'
add eax,ebx
add eax,'0' ;zamien na binarne z decymalnego
mov [wynik],eax
div wynik,'2'
add wynik,'0'
;pokaz sume
mov eax,4 ;
mov ebx,1
mov ecx,wynik
mov edx,8 ;8 to rozmiar tego wyniku z bss
int 0x80
exit:
mov eax,4
xor ebx,ebx
int 0x80

Assembly on Linux: unexpected behaviour from Assembly [duplicate]

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.

Assembly: trying to write to file, but text is appending to filename

I'm trying to study assembly, while trying out the example in the tutorials I get stuck. I am compiling this using an ubuntu virtual machine.
Here is the code:
SYS_READ equ 3
SYS_WRITE equ 4
SYS_OPEN equ 5
SYS_CLOSE equ 6
SYS_CREATE equ 8
SYS_EXIT equ 1
section .text
global _start
_start:
mov eax, SYS_CREATE
mov ebx, filename
mov ecx, 0777
int 0x80
mov [fd_out],ebx
mov eax,SYS_WRITE
mov edx,len
mov ecx,msg
mov ebx,[fd_out]
int 0x80
mov eax,SYS_CLOSE
mov ebx,[fd_out]
int 80h
mov eax,SYS_OPEN
mov ebx,filename
mov ecx,0
mov edx,0777
int 0x80
mov [fd_in],eax
mov eax, SYS_READ
mov ebx,[fd_in]
mov ecx,info
mov edx,26
int 0x80
mov eax,SYS_WRITE
mov ebx,1
mov ecx,info
mov edx,26
int 0x80
mov eax,SYS_EXIT
mov ebx,0
int 0x80
section .data
filename db 'test.txt'
msg db 'Hello world file'
len equ $-msg
section .bss
fd_out resb 1
fd_in resb 1
info resb 26
after executing the compiled output, I get a file named test.txtHello world file.
While SYS_WRITE accepts the length of the information in EDX, the function SYS_CREATE needs a pointer to NULL terminated string for the filename.
Your filename, defined as
filename db 'test.txt'
is not NULL terminated and that is why it concatenates with the next string:
msg db 'Hello world file'
The terminating NULL in this case is the next defined:
fd_out resb 1
In order to fix it, simply define the filename with zero terminating byte:
filename db 'test.txt', 0

Difference between EAX,1 and EBX,1 in assembly?

section .text
global _start ;must be declared for linker (ld)
_start: ;tell linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!',0xa ;our dear string
len equ $ - msg ;length of our dear string
Kernel source references:
How does the system know that it has to exit when it read EAX,1 and not EBX,1 ? Since 1 means Sys_Exit.
This comportement is defined in what we call ABI (application binary interface). This should help : What is Application Binary Interface (ABI)?

Resources