1

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
  • Which instruction is the error indicated for? Which of nasm and fasm are you programming for? – fuz Mar 16 '19 at 15:12
  • This is nasm 2.10.07 – Kamila Nowak Mar 16 '19 at 15:17
  • Which instruction is the error indicated for? – fuz Mar 16 '19 at 15:35
  • please, read the update, bcuz now the code is compiling – Kamila Nowak Mar 16 '19 at 16:05
  • 3
    @KamilaNowak: Please change the title of the question so it's relevant. Also, are you sure you want to use `RSP` as a general purpose register (and change RSP so that your stack is at the address 0x00000002)? – Brendan Mar 16 '19 at 16:59
  • Hi Kamila - 1st - the more effort you put in asking your question the more help you'll get so change as much of your native language text from the program (comments, variable names etc) into 'universal and omnipresent language of programming' which is english. 2nd when you read your numbers via system call #3 you get the result as sequence of ascii chars - to make it a number you can work with you must convert it to a number your CPU can work with, 3rd - do not use rsp - this is stack pointer register - not much harm here - just don't, 4th - you can divide by multiples of 2 just by right shift – Artur Mar 16 '19 at 18:15
  • see also [What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](//stackoverflow.com/q/46087730). The actual arithmetic error (from divide overflow) is a duplicate (at that point RDX >= 2 so RDX:RAX / 2 doesn't fit in RAX), but you really don't want to use the stack pointer to hold some temporary integer either. And like Artur said you should notice that you're dividing by a constant 2, and use `shr eax, 1` – Peter Cordes Mar 16 '19 at 18:24
  • 1
    I voted to reopen as the question was edited from the original problem which was **still** identified in the question's title. I rolled back to the original question. I did fix the tags as someone else had done so. The issue in the title matched the original code and the original comments. The `DIV` instruction takes one operand, not two. The issue with EDX was secondary (albeit important). Possibly this is a duplicate of another question that involves specifying more parameters than an instruction takes. This is a prime example of what goes wrong when people modify the code based on comments. – Michael Petch Mar 16 '19 at 20:57

0 Answers0