How to print a number in ARM assembly?

后端 未结 2 1755
眼角桃花
眼角桃花 2021-01-18 17:55

I am trying to print a number that I have stored. I\'m not sure if I am close or way off. Any help would be appreciated though. Here is my code:

.data
.ba         


        
2条回答
  •  隐瞒了意图╮
    2021-01-18 18:37

    Hi I appreciate that this is a pretty old thread but I've scratched my head over this for a while and would like to share my solution. Maybe it'll help someone along the way!

    I was aiming to print to digit without recourse to using C++ in any way, though I realise that simply decompiling a tostring() - or whatever equivalent exists in C++ - and seeing what that came up with would have been a far quicker route.

    Basically I ended up with creating a pointer to an empty .ascii string in the section .data and added the digit that I wanted to print + 48 to it before printing off that digit.

    The +48 of course is to refer to the specific digit's ascii index number.

    .global _start
    
    _start:
    MOV R8, #8
    ADD R8, R8, #48
    
    LDR R9, =num
    STR R8, [R9]
    
    MOV R0, #1
    LDR R1, =num
    MOV R2, #1
    MOV R7, #4
    SWI 0
    
    .data
    
    num:
    .ascii: " "
    

    The biggest drawback of this approach is that it doesn't handle any number more than one digit long of course.

    My solution for that was much, much uglier and beyond the scope of this answer here but if you've a strong stomach you can see it here:

提交回复
热议问题