TheStudentProgrammer
TheStudentProgrammer

Reputation: 53

Accessing and adding to a number in nasm

I am having trouble with this code. I have trying to add 16 to the number 656 while keeping the same structure of my code. I marked the parts I would like you to ignore. I am new to this website, if I should explain anything better please let me know. Thank you all for your time. This is the code that has been written. I think I am just lost in all the registers.

The goal is to have 656 stored in rbx and add 16 to 656 and replace that value in rbx.

        default rel
        section .text
        extern peek_byte
        extern read_byte
        extern write_byte
        extern raise_error
entry:
        mov rbx, rdi
        mov rax, 656
        mov [rbx + 0], rax
        mov rax, rbx
        or rax, 1
        add rbx, 8
        push rax
        mov rax, [rsp + 0] I would like to keep up to this the same.

        mov r9, rax
        and r9, 7
        cmp r9, 1
        jne raise_error
        xor rax, 1
        add rax, 16
        mov [rbx + 0], rax
        mov rax, rbx
        or rax, 1
        add rbx, 8
        mov rax, 120
        push rax
        
       mov rax, [rsp + 8] I do not wish to edit this part or beyond
        mov r9, rax
        and r9, 7
        cmp r9, 1
        jne raise_error
        xor rax, 1
        mov rax, [rax + 0]
        add rsp, 8
        add rsp, 8
        ret

Upvotes: 0

Views: 137

Answers (1)

TheStudentProgrammer
TheStudentProgrammer

Reputation: 53

Thank you to @jester. His reply helped me. I am going to show what parts got fixed in order to achieve the desired result

        mov r9, rax
        and r9, 7
        cmp r9, 1
        jne raise_error
        xor rax, 1
        mov rax, [rax + 0]
        mov r9, rax
        and r9, 15
        cmp r9, 0
        jne raise_error
        Add rax, 16
        mov [rbx + -8], rax <--
        mov rax, 120
        push rax``

Upvotes: 1

Related Questions