0

How to check if the result of a division is an integer and, depending on it, do the analog of if else construction in other languages? I've tried subtracting one int from another and checking if the result is lesser than the subtractor, but it looks very primitive to me.

EDIT: I'd be also grateful if somebody shows me how to set the element of the passed as the argument to an assembly function C array. For example:

    mov dd [rdx], 0

where rdx is a pointer to an array, produces a segmentation fault when called from C.

EDIT 2: I'm adding the code snippet (the commented lines are causing an error):

format ELF64

section '.text' executable

public have_int_solns

have_int_solns:
    ;rdi
    ;rsi
    ;rdx
    ;ecx
    dec ecx
    l1:
    mov eax, [rsi]
    mov ebx, [rdi]
    xor edx, edx
    div ebx
    cmp edx, 0
    je Equal
;     mov dword [rdx], 0
    jmp Both
    Equal:
;     mov dword [rdx], 1
    Both:
    add edi, 4
    add rsi, 4
    add rdx, 4
    loop l1
    ret

section '.data' writable
  • 2
    Do you mean if a number is a multiple of another? Or equivalently if a number is a divisor of another? `div` also gives you the remainder. – Margaret Bloom Nov 06 '20 at 16:11
  • @MargaretBloom, consider the following example: ```mov eax, 2 mov ebx, 4``` and I want to find if the value stored in ebx % the value stored in eax == 0. –  Nov 06 '20 at 16:13
  • 2
    Exactly, that's checking for "being multiple of". And the [`div`](https://www.felixcloutier.com/x86/div) and `idiv` instructions will give you the remainder (besides the quotient). If you search this site, [you'll find plenty of examples.](https://stackoverflow.com/questions/38416593/why-should-edx-be-0-before-using-the-div-instruction) – Margaret Bloom Nov 06 '20 at 16:22
  • @MargaretBloom Thank you. I got additional question when resolving the previous problem and added it to the question. It would be great if you know the answer to it too. –  Nov 06 '20 at 17:02
  • A segfault suggests that the address that you use is invalid. – 500 - Internal Server Error Nov 06 '20 at 18:00
  • @500-InternalServerError I know, but I'm trying to understand why –  Nov 06 '20 at 18:08
  • Fair enough, but we can't help you with that since you're not showing how you set it up. – 500 - Internal Server Error Nov 06 '20 at 18:11
  • @500-InternalServerError I've added the code –  Nov 06 '20 at 18:17
  • 1
    For the `equal` case here you would effectively execute `mov dword [0], 1` - not allowed in most contexts, address zero is off limits, and probably not what you meant either. So what _did_ you intend to do here in your own words? – 500 - Internal Server Error Nov 06 '20 at 18:20
  • @500-InternalServerError Yes, this was the problem. But now the next problem arises: my task is to call this function, which checks if the the element from the first array divided by the element from the second one is an integer, from a c file. I pass 4 parameters: 3 arrays and their length. The pointer to the third array is passed to the rdx register, which is also used for storing the remainder of a division. What do? –  Nov 06 '20 at 18:27
  • 3
    You have three basic options: 1) save off the value in `edx` in an unused register if you have one, 2) save the value in a local stack slot that you must then reserve up front (with something like `sub esp,4`, or 3) push the register to the stack before using it in the division and then pop it off again afterwards. – 500 - Internal Server Error Nov 06 '20 at 18:37
  • @500-InternalServerError Thanks, I've used the available register –  Nov 06 '20 at 19:06
  • 1
    `mov dd [rdx], 0` isn't valid FASM syntax. Perhaps you mean `mov dword [rdx], 0`? There's no way it can segfault if you wan't even assemble it first. – Peter Cordes Nov 07 '20 at 04:19

0 Answers0