0

According to some tutorials I've seen, there's a number of ways to do things in Assembly. I'm at a marker here on whether I can do this or not. I'm ordering a book on Assembly, but right now I need to know the thing that is wrong with my High - Low game. It's meant to find the number of the user's choice. Besides the fact that I don't have it really doing much of anything yet, I'm stuck on a Floating Point Exception. I was using 32-bit registers, and I decided to try and move them to 64-bit, but that didn't help at all. I'm expecting it to run, and continue running until they do not press 'Y' or 'N' at the input point. Right now, as I said, it comes up with the FPEx and dumps without any user input.

I've tried Tutorials on YouTube, and some cheat sheets. But they just don't have the ability to confine me to the area where my bug is. I got as far as I am on running through those though. Very helpful, but I need to get some 1:1 right now.

    global _start
section .data
message: db "High Lo", 10
m_len equ $-message
hot_cold: db "Am I getting warmer? (Y/N/Quit): "
w_len equ $-hot_cold
yes: db "Y"
no: db "N"
divide: dq 4.0
multiply: dq 2.0

    section .bss
hilo: resq 1
NoHi: resq 1
NoLo: resq 1
newGuess: resq 1
yn: resd 1

    section .text

_start:
    mov rax, 1
    mov rdi, 1
    mov rsi, message
    mov rdx, m_len
    syscall
    mov dword [NoHi], 50
    mov dword [NoLo], 50

.loop:

    mov rcx, [yes]      ; char compare Y
    mov rbx, [no]       ; char compare N

    xor rax, rax
    xor rdi, rdi
    xor rsi, rsi
    xor rdx, rdx
    syscall

    mov rax, 1
    mov rdi, 1
    mov rsi, hot_cold
    mov rdx, w_len
    syscall

;   xor rsi, rsi
    xor rsp, rsp        ; clear out register with y/n
    xor rax, rax
    xor rdx, rdx
    xor rbp, rbp

    mov rax, 0      ; setup for input
    mov rbx, 0      ;
    mov rsi, rsp        ; collect input
    mov rdx, 2      ; how many inputs
    syscall         ; init collection

.hop_yes:
    cmp rcx, rsp        ; compare to Y
    jne .hop_no     ; compare fail, try N
    call _for_y     ; compare success, change numbers
    ;syscall
    jmp .loop       ; loop on successful Y

.hop_no:
    cmp rbx, rsp        ; compare to N
    jne _exit       ; compare fail, rxit
    call _for_n     ; compare success change numbers
    ;syscall
    jmp .loop

_for_y:

    xor rax, rax
    xor rdx, rdx
    ;xor rcx, rcx
    mov rax, [NoHi]
    mov rdx, [multiply]
    mul rdx         ; byte [multiply]
    mov rdx, rax
    syscall
    ;-----
    xor rdi, rdi
    ;xor rdx, rdx
    syscall
    mov rax, 1
    mov rdi, 1
    mov rsi, rdx
    ;mov rdx, 3
    syscall
    ;-----
    xor rdx, rdx
    mov rax, [NoLo]
    mov rdx, [divide]
    div rdx
    mov rbp, rax
    mov [NoLo], rbp
    syscall
    ret

_for_n:
    xor rdx, rdx
    mov rax, [NoLo]
    mov rdx, [divide]
    div rdx
    mov [NoLo], rax
    ;-----
    xor rax, rax
    mov rax, [NoHi]
    mov rdx, [multiply]
    mul rdx
    mov [NoLo], rax
    syscall

    ret
onex4
  • 3
  • 3
  • 1
    You're defeating the purpose of `xor rdx,rdx` by loading the divisor into it before `div`! And BTW, a debugger would have pinned down the faulting instructions as being `div` so you could search on that. – Peter Cordes Mar 09 '20 at 03:21
  • I passed that goal of getting by the FPEx. Now it's just exiting at that point now. – onex4 Mar 09 '20 at 03:47
  • Ok, so debug your code with a debugger. e.g. single-step and watch register values. e.g. doing a 64-bit (qword) load from `[yes]` is unlikely to be what you want, and a debugger can show you what you actually get in a register from loading 8 bytes starting at that label. – Peter Cordes Mar 09 '20 at 04:03
  • Why did you load a float `4.0` to `RDX` and use it for integer division? This is wrong but it not the reason for the Floating Point Exception. `DIV R_X` means dividing `RDX:RAX` by `R_X`. When 'RDX` >= 'R_X', the result will be no less than `0x1_0000_0000`, which is too big and can never be fitted in to `RAX`, leading to the exception. Since `RDX` is always >= `RDX`, `DIV RDX` always causes a 'divide by zero' or floating point exception. – W. Chang Mar 09 '20 at 10:29

0 Answers0