ARM assembler for ARM learning

ARM assembly language program structure In ARM (Thumb) assembly language program, the code is organized by program segments. Segments can be divided ...
ARM assembly language program structure
Assembly language subroutine call

ARM assembly language program structure

In ARM (Thumb) assembly language program, the code is organized by program segments. Segments can be divided into code sections and data sections. A assembler program should have at least one code segment. When the program is long, it can be divided into multiple code segments and data segments. When the program is compiled and linked, multiple segments eventually form an executable image file.
The composition of the executable image file:


The linker arranges the segments in the memory according to the system default or user set rules. Therefore, the relative position between the segments of the source program and the executable image file is generally not the same.

Assembly language subroutine call

In ARM assembly language program, the call of subroutine is usually realized by BL instruction.

bl DELAY ;DELAY Subroutine name ... ldr r0 ,= 0xffff DELAY: sub r0,r0,#1 ;r0 = r0 - 1 cmp r0,#0; compare if the value of r0 is equal to 0 bne DELAY ;If it's not equal, it's back DELAY implement ldr r1 ,= 0x08 ...

(1) when the subprogram is executed, the return address of the subprogram should be saved in the connection register LR, and the program counter PC points to the entry point of the subprogram.
(2) the subroutine needs to be returned after execution, only the return address stored in LR needs to be copied back to the program counter PC.
(3) at the same time of calling subroutine, it can also transfer parameters and return operation results from subroutine. Generally, it can be completed by register R0~R3

Compilation example

Assembly implementation string copy:
Compilation environment: Keil4

area init,code,readonly entry ldr r0,= datablock1 ;Get the address of the string and save it to r0 in ldr r2,= datablock2 ;Get the address of the allocated memory unit with the size of 100 and save it to r2 in cat ldrb r1,[r0],#1; take out 1 byte of data and store it in r1, r0 address plus 1 strb r1,[r2],#1. Save the 1 byte data to r2, r2 address plus 1 cmp r1, #0; compare whether the extracted string character is' \ 0 ' bne cat ;If not, continue copying b . datablock1 dcb "hello young man!",0 datablock2 space 100 ;Allocate 100 size memory units end ;Marking the end of the program

Get student's highest score:

;init.s area init,code,readonly ;The definition has a name init Code segment(code) Property is read-only entry import getmax import scores import maxscore import numofstudent ; mov r0,#1 ; mov r1,#2 ; cmp r1,r0 ; movhi r0,r1 ldr r2,=scores ldr r4,=numofstudent ldrb r5,[r4] ;Ten students ldrb r0,[r2] ;Take out the scores of the first student add r2,r2,#1 sub r5,r5,#1; take out the first student, minus 1 bl getmax ldr r4,= maxscore strb r0,[r4] b . end
;maxoftwo.s ;Two numbers to compare are r0,r1 in ;The maximum number of comparisons is r0 Among area max,code,readonly import scores ;statement scores From outside export getmax ;statement getmax Can be called externally getmax ldrb r1,[r2],#1. Take out the second student's score sub r5,r5,#Take out another student's score, and reduce 1 cmp r1,r0 movhi r0,r1 ; Compare, put the high scores in r0 in cmp r5,#0; judge whether ten students have finished bne getmax ;Not to continue bx lr end
;score.s export scores ;Declaration may scores Called externally export numofstudent export maxscore area score ,data,readwrite ;Define data segment, readable and writable scores dcb 65,78,92,47,77,83,59,93,82,97 ;Student achievement numofstudent dcb 10 ; Number of students maxscore dcb 0;//Store maximum score end

18 November 2019, 09:55 | Views: 8809

Add new comment

For adding a comment, please log in
or create account

0 comments