Invalid combination of opcode and operands in program assembly - linux

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

Related

Reading multiple lines of input

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

Unable to get the correct .ascii sum

ok,I'm now on assembly.I was making some programms to understand how ascii,hex,binary and system calls works, but actually i came across this problem:
when i input 11(1+1) for example,b is the result and not 2. Why?
SECTION .bss:
Num1: db 1
Num2: db 1
Ris db 1
Read:
mov eax,3
mov ebx,0
mov ecx,Num1
mov edx,1
int 80h
mov eax,3
mov ebx,0
mov ecx,Num2
mov edx,1
int 80h
mov edx,[Num1]
mov ebx,[Num2]
add ebx,edx
mov [Num2],ebx
Write:
mov eax,4
mov ebx,1
mov ecx,Num2
mov edx,1
int 80h
and why if i input only num 1 and when I try to sum it in this way the result is correct?
when i input 1(1+3) for example,4 is the result and not d(how wold it have been in the first case). Why?
SECTION .bss
Num1: db 1
Num2: db 1
Ris db 1
;inpb: resb 1
SECTION .data
SECTION .text
global main
Read:
mov eax,3
mov ebx,0
mov ecx,Num1
mov edx,1
int 80h
; mov eax,3
; mov ebx,0
; mov ecx,Num2
; mov edx,1
; int 80h
mov edx,[Num1]
mov ebx,3 ;rather than Num2 i put 3
add ebx,edx ;sum beetwen Num 1 and 3
mov [Num2],ebx
Write:
mov eax,4
mov ebx,1
mov ecx,Num2
mov edx,1
int 80h

Assembly NASM - AND Mask

When I run this program it says:
jdoodle.asm:9: error: invalid combination of opcode and operands
The problem is the AND al, ah. The rest of the code should be correct, I just need to know how to solve this problem because as it seems I can't do an AND between 2 registers.
section .text
global _start
_start:
call _input
mov al, input
mov ah, maschera
and al, ah
mov input, al
call _output
jmp _exit
_input:
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, 1
int 80h
ret
_output:
mov eax, 4
mov ebx, 1
mov ecx, input
mov edx, 1
int 80h
ret
_exit:
mov eax, 1
int 80h
section .data
maschera: db 11111111b
segment .bss
input resb 1
MASM/TASM/JWASM syntax is different from NASM. If you want to load/store data at an address you need to explicitly use square brackets. If you want to use the MOV instruction to place the address of a label in a variable you do not use square brackets. Square brackets are like a de-reference operator.
In 32-bit code you will want to ensure addresses are loaded into 32-bit registers. Any address above 255 won't fit in an 8 byte register, any address above 65535 won't fit in a 16-bit register.
The code you were probably looking for is:
section .text
global _start
_start:
call _input
mov al, [input]
mov ah, [maschera]
and al, ah
mov [input], al
call _output
jmp _exit
_input:
mov eax, 3
mov ebx, 0
mov ecx, input
mov edx, 1
int 80h
ret
_output:
mov eax, 4
mov ebx, 1
mov ecx, input
mov edx, 1
int 80h
ret
_exit:
mov eax, 1
int 80h
section .data
maschera: db 11111111b
segment .bss
input resb 1

NASM unexpected output when printing

I am new have very small problem with assembly NASM in linux. I made simple program for practice that when you put in the text, it adds simple decoration in form of stars. The expected output is:
*********EXAMPLE*********
instead:
*********EXAMPLE
*********
here is the complete code of the program (long) i have use edb to check the code and check EDX register if it match the len take by the null byte check to print correct number of characters.
section .data
prompt db "Please enter a word (MAX: 10 Characters) : ", 0xa, 0xd
plen equ $ - prompt
stars times 9 db "*"
section .bss
text resb 10
section .text
global _start
_start:
mov eax, 4
mov ebx, 1
mov ecx, prompt
mov edx, plen
int 0x80
mov eax, 3
mov ebx, 0
mov ecx, text
mov edx, 11
int 0x80
xor ecx, ecx
mov esi, text
mov ecx, 0
loop1:
inc ecx
cmp byte [esi + ecx], 0x00
jne loop1
push ecx
jmp printexit
printexit:
mov eax, 4
mov ebx, 1
mov ecx, stars
mov edx, 9
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, text
pop edx
int 0x80
mov eax, 4
mov ebx, 1
mov ecx, stars
mov edx, 9
int 0x80
mov eax, 1
int 0x80

Assembly language taking input from user but displaying output

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

Resources