最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

My ARM Assembly code, which is meant to calculate the sums of all odd and even numbers up to an input integer, is returning a Se

programmeradmin1浏览0评论
.global main
.extern printf
.extern scanf

.section .data
prompt: .asciz "Enter an integer between 2 and 100: "
error_msg: .asciz "Invalid input. Exiting.\n"
even_msg: .asciz "The even numbers from 1 to %d are:\n"
odd_msg: .asciz "The odd numbers from 1 to %d are:\n"
sum_even_msg: .asciz "The even sum is: %d\n"
sum_odd_msg: .asciz "The odd sum is: %d\n"
int_format: .asciz "%d\n"
scanf_format: asciz "%d"
.section .bss
input_num: .space 4

.section .text
main:
    ldr r0, =prompt
    bl printf

    ldr r0, =scanf_format
    ldr r1, =input_num
    bl scanf

    ldr r2, =input_num
    ldr r3, [r2]
    cmp r3, #2
    blt invalid_input
    cmp r3, #100
    bgt invalid_input

    ldr r0, =even_msg
    ldr r1, [r2]
    bl printf

    mov r4, #2
    mov r5, #0

even_loop:
    cmp r4, r3
    bgt print_even_sum
    ldr r0, =int_format
    mov r1, r4
    bl printf
    add r5, r5, r4
    add r4, r4, #2
    b even_loop

print_even_sum:
    ldr r0, =sum_even_msg
    mov r1, r5
    bl printf

    ldr r0, =odd_msg
    ldr r1, [r2]
    bl printf

    mov r4, #1
    mov r5, #0

odd_loop:
    cmp r4, r3
    bgt print_odd_sum
    ldr r0, =int_format
    mov r1, r4
    bl printf
    add r5, r5, r4
    add r4, r4, #2
    b odd_loop

print_odd_sum:
    ldr r0, =sum_odd_msg
    mov r1, r5
    bl printf

    mov r0, #0
    b exit

invalid_input:
    ldr r0, =error_msg
    bl printf
    mov r0, #1
    b exit

It allows me to input a number, prints the even sum messages with 0 in place of d%, then returns an error. I've already fixed some errors in regards to misassigning my scanf and integer formats, as well as syntax errors regarding printing the integer input. I'm assembling and running the code on a Raspberry Pi, with the C library exported in. I've managed to successfully assemble and link the code, and have tried using gdb, but I'm not super familiar with ARM (or any assembly code), so I'm having issues even seeing where the problem might be.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论