E
E
EEElice2021-03-13 00:39:45
assembler
EEElice, 2021-03-13 00:39:45

I don't understand what happens inside the register after the DIV. "Registers break"?

There is a small code for NASM, in which some operations are performed on the input variables: (x + 1) / y.
The problem is that after division, an attempt to deduce the value of the quotient / remainder from the register ends with the output of a goat.
I don't understand why it behaves this way even for simple values ​​(x,y = 3.2). Actually, this is the point, so that it works at least for such small values.

SYS_EXIT  equ 1
SYS_READ  equ 3
SYS_WRITE equ 4
STDIN     equ 0
STDOUT    equ 1

section .data
   inp1msg db "enter x:", 0xA,0xD
   len1 equ $- inp1msg
   
   inp2msg db "enter y:", 0xA,0xD
   len2 equ $- inp2msg
   
   outmsg db "result:"
   len3 equ $- outmsg
 
section .bss
   x resb 8
   y resb 8
   z resb 8
   
section .text
   global _start
  
_start: 

   mov eax, SYS_WRITE         
   mov ebx, STDOUT         
   mov ecx, inp1msg        
   mov edx, len1 
   int 0x80                
 
   mov eax, SYS_READ ;вводим x
   mov ebx, STDIN  
   mov ecx, x
   mov edx, 8
   int 0x80            
 
   mov eax, SYS_WRITE;вводим у
   mov ebx, STDOUT         
   mov ecx, inp2msg         
   mov edx, len2         
   int 0x80
 
   mov eax, SYS_READ  
   mov ebx, STDIN  
   mov ecx, y 
   mov edx, 8
   int 0x80        
 
   mov eax, SYS_WRITE         
   mov ebx, STDOUT         
   mov ecx, outmsg         
   mov edx, len3         
   int 0x80
   
   ;перемещаем переменные x y в ax bx соответственно
   mov ax, [x]
   mov bx, [y]
   
   ;1 x+1 (увеличиваем x на 1)
   inc ax
   ;2 (x+1)/y делим на у, все проблемы начинаются отсюда
   div bx

   mov [z], ax
   
   mov eax, SYS_WRITE
   mov ebx, STDOUT
   mov ecx, z
   mov edx, 1
   int 0x80
  
exit:    
   
   mov eax, SYS_EXIT   
   xor ebx, ebx 
   int 0x80


The following is output:
enter x:
3
enter y:
2
result:�


If you comment out the division, then the output of the result x + 1 will be normal:
enter x:
3
enter y:
2
result:4


That is, this is a problem in the division itself. The output of registers al (like ax in the first snippet) also gives a goat, ah outputs emptiness:
enter x:
3
enter y:
2
result:�

enter x:
3
enter y:
2
result:

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-03-13
@EEElice

The numbers from read() come in the form of strings - sequences of ASCII codes. For 2 and 3 it will be 0x32 and 0x33 respectively (I have already forgotten, to be honest, whether they will come along with "\n" or not).
In short, you are sharing ASCII codes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question