0

I'm currently learning Assembly (Intel x64) for my machine (Ubuntu if that matters). (Mind you, it isn't NASM).

For the life of me, I can't find a way to compare a register to a char. Here's my code:

.global _start
.intel_syntax noprefix

_start:
        // Print Question
        mov rax, 1
        mov rdi, 1
        lea rsi, [.the_ask]
        mov rdx, 28
        syscall
        // Load Input into memory
        mov rax, 0
        sub rsp, 8
        mov rdi, 0
        lea rsi, [rsp]
        mov rdx, 1
        syscall
        // Print Newline
        mov rax, 1
        mov rdi, 1
        lea rsi, [.newline]
        mov rdx, 2
        syscall
        // Comparison (hopefully)
        lea r8, [.dogletter]
        lea r9, [.catletter]
        cmp rsp, r9
        je _docatstuff
        cmp rsp, r8
        je _dodogstuff

        // Exit
        mov rax, 60
        mov rdi, 1
        syscall

_docatstuff:
        // meow at user
        mov rax, 1
        mov rdi, 1
        lea rsi, [.catstuff]
        mov rdx, 4
        syscall
        // newline
        mov rdx, 2
        lea rsi, [.newline]
        syscall
        // exit
        mov rax, 60
        mov rdi, 0
        syscall

_dodogstuff:
        // bark at user
        mov rax, 1
        mov rdi, 1
        lea rsi, [.dogstuff]
        mov rdx, 4
        syscall
        //newline
        mov rdx, 2
        lea rsi, [.newline]
        syscall
        //exit
        mov rax, 60
        mov rdi, 0
        syscall

.the_ask:
        .asciz "Please Enter: (c)at, (d)og: "

.newline:
        .asciz "\n"

.catletter:
        .asciz "c"

.dogletter:
        .asciz "d"

.catstuff:
        .asciz "meow"

.dogstuff:
        .asciz "woof"

I already know my implementation is wrong, I want to know how to implement this correctly please.

Any help would be greatly appreciated!

What I've Tried: Comparing like: cmp rsp, "c", Comparing rsp to the ASCII value of the letter: cmp rsp, 67, and I have lastly tried the attempt that you see in my current source code.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
synnøve
  • 47
  • 5
  • 2
    You're comparing pointers, not char values (or whole strings which takes multiple instructions in a loop). One `cmp` instruction can have at most one memory operand, so you want `movzx r8d, byte ptr [rip + .dogletter]` / `cmp [rsp], r8b` or similar. Or `cmp byte ptr [rsp], 'c'` To have a compiler make an example for you, call a function like scanf or `read` that similarly reads into a local buffer on the stack. [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) – Peter Cordes Aug 25 '23 at 23:19
  • Thank you so much for the clarification, and also for clearing up the tags, it is greatly appreciated! – synnøve Aug 25 '23 at 23:30
  • Study the control structure of if-then-else in assembly. It is virtually the same in all assembly languages, so you don't necessarily need to learn from only one assembler/assembly language. – Erik Eidt Aug 26 '23 at 01:15
  • Yeah, it's going to take a little bit of getting used to. Thank you – synnøve Aug 26 '23 at 12:53
  • 1
    Review this answer, as it decomposes if-then-else: https://stackoverflow.com/a/72693547/471129 – Erik Eidt Aug 26 '23 at 14:30
  • Write your algorithm in C or other language you know and then translate to assembly as literally as possible; each control structure (if-then, while, for) has a simple translation to assembly; so do expressions involving dereferencing, indexing, and comparison. Best to fully work out your algorithm before taking it to assembly: assembly is much easier if you start with a working algorithm. – Erik Eidt Aug 26 '23 at 14:37

0 Answers0