0

I've seen many questions like this but none seem to work for my use case. I don't understand the C integration and have never coded in C, and can't seem to understand the REPE commands. I'm trying to check if a user has inputted an "F" or a "D" and then take an input for the temperature and convert to either degrees or Fahrenheit.

I am however only at the point of checking the user input, which has proven difficult with the cmp function. I have made a data structure with the value of 70, to compare with F as this is the ASCII value. I then move this data into RAX and the data from the user input into RSI, using square brackets. Using GDP, I can see that if I "print (char)" at the memory address, they are the same. However, printing and casting to int returns different values which I am guessing may be the issue.

I will attach below the relevant code.

section .data
    introChoice db "Please choose Fahrenheit or Celsius (C/F): ",10,0
    introNum db "Please enter a temperature (max. 3 digits) : ",10,0
    error db "Please enter in the correct format.",10,0
    fahr db 70,0
    celsius db "c",0
section .bss
    degree resb 8
    celfar resb 8
    digitSpace resb 8
    digitSpacePos resb 8
section .text
    global _start

_start:
    mov rax, introChoice
    call _print
    call _getCelFar
    mov rax, [fahr]
    mov rsi, [celfar]
    cmp rax, rsi
    jz _test

Further on, just to show my test, print and get input functions (print function from kupala)

_test:
    mov rax, introChoice
    call _print_print:
    push rax
    mov rbx, 0
_printLoop:
    inc rax
    inc rbx
    mov cl, [rax]
    cmp cl, 0
    jne _printLoop

    mov rax, 1
    mov rdi, 1
    pop rsi
    mov rdx, rbx
    syscall

    ret
_getDegree:
    mov rax, 0
    mov rdi, 0
    mov rsi, degree
    mov rdx, 100
    syscall
    ret
_getCelFar:
    mov rax, 0
    mov rdi, 0
    mov rsi, celfar
    mov rdx, 100
    syscall
    ret

I'll also attach the output from checking the registers in gdb here.

_start () at degree.asm:19
19      mov rax, [fahr]
(gdb) s
20      mov rsi, [celfar]
(gdb) s
21      cmp rax, rsi
(gdb) i r
rax            0x630046            6488134
rbx            0x2c                44
rcx            0x401138            4198712
rdx            0x64                100
rsi            0xa46               2630
rdi            0x0                 0
rbp            0x0                 0x0
rsp            0x7fffffffe110      0x7fffffffe110
r8             0x0                 0
r9             0x0                 0
r10            0x0                 0
r11            0x346               838
r12            0x0                 0
r13            0x0                 0
r14            0x0                 0
r15            0x0                 0
rip            0x401024            0x401024 <_start+36>
eflags         0x246               [ PF ZF IF ]
cs             0x33                51
ss             0x2b                43
ds             0x0                 0
es             0x0                 0
fs             0x0                 0
gs             0x0                 0
(gdb) print (char) 0x630046
$1 = 70 'F'
(gdb) print (char) 0xa46
$2 = 70 'F'
(gdb) 

Any help would be much appreciated, I hope it's something silly I'm missing but I've been trying to do this for a long time now.

  • 1
    separate from the programming question, but https://en.wikipedia.org/wiki/Fahrenheit "uses the degree Fahrenheit (symbol: °F) as the unit". So "degrees" doesn't rule out Fahrenheit. Much more sane would be C or F as valid inputs. You might as well ask the user if they want their distance in "inches or length" instead of "inches or cm" – Peter Cordes Mar 13 '21 at 16:45
  • 1
    `mov rax, [fahr]` that loads 8 bytes, but `db 70` only emits 1 byte. To load a byte, use `movzx eax, byte [fahr]`. Probably you have the same problem with strings, too, loading more than 1 byte so they don't match a `'F'`. – Peter Cordes Mar 13 '21 at 16:47
  • 1
    Fun physics fact: the only common temperature unit (AFAIK) that *isn't* degrees is the Kelvin scale, where we say "hotter by 2 Kelvins". There is no such thing as a degree Kelvin. (And it's an absolute scale so it's physically meaningful to say something is "twice as hot" = 2x the Kelvin temp.) – Peter Cordes Mar 14 '21 at 02:03

0 Answers0