Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using GNU Assembler and I am trying to learn aarch64 assembly by making a simple calculator, I cannot store the result value inside a data called results

.global main

.section .data

debug_msg1: .asciz "True"
debug_msg1_len = . - debug_msg1

format: 
    .ascii "results is %d\n"
format_len = 10

message_0:
    .asciz "Hello, World!\n"
message_0_len = . - message_0


message_1:
    .asciz "Enter mode +,-,*,/: "
message_1_len = . - message_1

message_2: 
    .asciz "enter the first num: "
message_2_len = . - message_2

message_3: 
    .asciz "enter the second num: "
message_3_len = . - message_3

message_4: 
    .asciz "results is "
message_4_len = . - message_4

.section .bss
    num1: .skip 10
    num2: .skip 10
    operator: .skip 1
    .lcomm results, 10
.section .text
main:
    // Writes "Hello, world!"
    ldr x1, =message_0  // Load the address of the message
    ldr x2, =message_0_len       // Length of the message
    bl print
    
    /* prompts mode */
    ldr x1, =message_1
    ldr x2, =message_1_len
    bl print

    // takes input
    ldr x1, =operator
    ldr x2, =10
    bl input
    
    /* fake input to handle enter press
    ldr x1, =temp
    ldr x2, =1
    bl input*/
    /* end prompt mode */

    /* prompts first num */
    ldr x1, =message_2
    ldr x2, =message_2_len
    bl print
    
    // takes input
    ldr x1, =num1
    ldr x2, =10
    bl input
    /* end prompt first num */

    /* prompts second num */
    ldr x1, =message_3
    ldr x2, =message_3_len
    bl print
    
    // takes input
    ldr x1, =num2
    ldr x2, =10
    bl input
    /* end prompt second num */
    //prints operator
    /* results */

    ldr x1, =operator
    
    //prints x1 to make sure its correct
    ldr x2, =20
    bl print
    

    
    ldrb w0, [x1]
    sub w0, w0, 0x0 // refresh w0 so it doesnt change when x1 change
    
    ldr x1, =num1
    ldrb w3, [x1]
    sub w3, w3, '0' // converts results to number && ^^

    ldr x1, =num2
    ldrb w4, [x1]
    sub w4, w4, '0' // ^^
    
    cmp w0, #'+'
    
    beq add
    /* end results */

    // Exit the program
    bl exit
print:
    mov x0, 1 // file descriptor STDOUT
    mov w8, 64 // sys call sys write
    svc #0 // invoke syscall
    
    ret // branch back to [lr](in this case its _start)

input: 
    mov x0, 0 // file descriptor STDIN
    mov w8, #63 // syscall sys read
    svc #0
    
    
    ret
add: 
    ldr x1, =results
    
    add w3, w3, w4
    
    str w3, [x1] 
    sub sp, sp, #16

    ldr x0, =format
    
    ldr x2, =10
    bl printf

    add sp, sp, #16

    bl exit
exit: 
    mov w8, #93 // syscall exit
    mov x0, #0 // exit status
    svc #0
true:
    ldr x1, =debug_msg1
    ldr x2, =debug_msg1_len
    bl print
    ret
exit_err:
    mov w8, #93
    mov x0, #1
    svc #0


What I have tried:

- using different registers
- different data types for results
Posted

Are you using, or have access to a debugger?
If not put a print at the beginning of the add and make sure you are actually getting there. Your add code looks like it should work.

I don't understand why you're doing this, it will fall through to exit no matter with or without the BL.
ASM
    bl exit
exit: 
    mov w8, #93 // syscall exit
    mov x0, #0 // exit status
    svc #0
 
Share this answer
 
Comments
Atonix0 16-Sep-23 9:23am    
The bl part is for no reason and I know I could just do b;
I have access to a debugger and my add code works fine the only problem is with storing the data...
Atonix0 16-Sep-23 9:33am    
WTF I swear it didn't work yesterday now it works fine but I have another problem it seems like it can only see first num if I put + 66 67 as input it returns 12
Atonix0 16-Sep-23 9:34am    
I believe it was a memory problem...
Atonix0 16-Sep-23 10:47am    
It turns out I solved it yesterday but I forgot to compile that because I was sleepy thanks for trying to help...
Fixed it by directly storing results in W1 like this
add W1, W2, W3

Instead of
add w3, w2, W3
str W3, [X1]
<pre lang="ASM">
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900