当前位置:网站首页>[arm assembly judgment] how to use assembly to judge the number of positive and negative numbers in an array?
[arm assembly judgment] how to use assembly to judge the number of positive and negative numbers in an array?
2022-04-21 08:29:00 【Im Tom Kate】

Hello everyone , I'm Tom Kate .
List of articles
️ Author's brief introduction : Hello, I'm Tom Kate , You can call me Tom
Personal home page :IM Tom Kate's CSDN Blog
Series column :【ARM Embedded foundation 】
One sentence per day :
How a person treats his time , Determines what kind of person he can be .
【ARM assembly 】 How to determine the number of positive and negative numbers in an array ?
Write it at the front :
It must be used in many algorithms if Sentence to judge , We mentioned arrays earlier , I also practiced traversing the array with the assembly loop framework , But when we use arrays , It is impossible to just traverse the array and find the sum of the array . When we do some other operations on the array , You need to use judgment statements , What are the judgment statements in the assembly ?
All judgment suffixes
| stay x86 In, we can only use the jump command `“B”` Add a judge after , And in the ARM In, we can add the judgment mark after any instruction |
All judgment suffixes , I marked the commonly used ones red :

Let's look at today's question
How to use assembly to judge the number of positive and negative numbers in an array ?
In assembly language, the basic framework of the program is unchanged , We've written the framework of the loop before , Here we can directly post the forward traversal program , Then we think about how to use the judgment suffix to separate the positive and negative numbers in the array , Judge the number and sum them separately .
1. Preparation before Compilation
1.1 use C Pseudo code representation of
| First use C Language to achieve this function , It is convenient to clarify the idea of realizing the function |
C The language code :
#include<stdio.h>
int main(){
int ary[10] = {
0,-1,2,3,4,5,-6,7,8,9};
int sum1=0,sum2=0, i;
int z=0,f=0;
for ( i = 0; i < 10; i++)
{
if(ary[i]>0){
z=z+1;
sum1 =sum1 +ary[i];
}
else if(ary[i]<0){
f=f+1;
sum2 =sum2 +ary[i];
}
}
printf(" The number of positive numbers is :%d\n The number of negative numbers is :%d\n",z,f);
printf(" The sum of positive numbers is :%d\n The sum of negative numbers is :%d\n",sum1,sum2);
return 0;
}
Running results :

1.2 Refer to the framework of forward traversal
| The quickest way to learn anything is , Take the knowledge already in the brain , Compare and extend new knowledge with old knowledge . |
Paste the forward traversal program , It is found that the framework for realizing the overall function is basically the same , Just change this part of the loop . Take the steps apart , It is subdivided into the following steps , Let's see .

2. To write ARM assembly
2.1 First give a string of arrays 、 Output format
| Here, try to output the number of positive numbers 、 The number of negative numbers and the sum of positive numbers 、 The sum of negative numbers ; Array with ary Customize ; Calculate the number of times to cycle through the address length |
.data
fmt1:.asciz " The number of positive:%d\n The number of negative:%d\n"
fmt2:.asciz " The positive and:%d\n The negative and:%d\n"
ary:.word 0,-1,2,3,4,-5,6,7,8,-9
.equ counter,( . - ary)/4
2.2 Add judgment to the loop 、 Count
This step is the core step , Prepared by foreign exchange if Judgment statement . All instructions given before , among CMP Used to determine . And CMP Closely linked is the instruction with judgment suffix , This command can be a jump (B) It can be (ADD) It can also be (MOV).
| Functions to be implemented : Take out the first value in the array , Then judge with zero , When greater than zero , Add one... To a positive counter , And add the current value to the cumulative sum of positive numbers . When it is judged to be negative, it is the opposite . |
ARM Assembly implementation method :
ldr r2,[r5],#4
cmp r2,#0
addgt r7,#1
addgt r9,r2
addlt r8,#1
addlt r10,r2
If in x86 It can only be CMP Comparison is used up BLT Jump to subroutine , Jump back after execution ,ARM It's much easier to judge , It can be directly added to the statement you want to execute after you want to compare . And only one comparison is needed . It can be followed by many , Compare the code you want to execute .
2.3 Initialize the registers used in the main function
Generally, the definition of registers is after the completion of the core code , Check which registers are used in the core code , Then assign the initial value to the corresponding register
The registers here represent :
r7 The number used to store positive numbers
r8 The number used to store negative numbers
r9 Used to store the sum of positive numbers
r10 Used to store the sum of negative numbers
What we use here R4 and R5 The same as before, no change is still : Loop variable and array first address
mov r4,#0
mov r7,#0
mov r8,#0
mov r9,#0
mov r10,#0
ldr r5, =ary
2.4 The sum of output accumulation
-
If you want to print out, you must use printf, Then we have to First call the output format string , The format string has been defined at the beginning .
-
printf It's from R1 Start to output So we need to count the number of positive and negative values 、 Positive and negative numbers and , Pass to R1、R2.
-
The following part has been printed , So we can R1、R2 Cover , also When used B Jump once ,R0~R3 All the values of will change , It's best to rewrite .
ldr r0,=fmt1
mov r1,r7
mov r2,r8
bl printf
ldr r0,=fmt2
mov r1,r9
mov r2,r10
bl printf
2.5 Source code
.data
fmt1:.asciz " The number of positive:%d\n The number of negative:%d\n"
fmt2:.asciz " The positive and:%d\n The negative and:%d\n"
ary:.word 0,-1,2,3,4,-5,6,7,8,-9
.equ counter,( . - ary)/4
.text
.globl main
main:
stmfd sp!,{
lr}
mov r4,#0
mov r7,#0
mov r8,#0
mov r9,#0
mov r10,#0
ldr r5, =ary
b testfor
loop:
ldr r2,[r5],#4
cmp r2,#0
addgt r7,#1
addgt r9,r2
addlt r8,#1
addlt r10,r2
add r4,#1
testfor:cmp r4,#counter
blt loop
ldr r0,=fmt1
mov r1,r7
mov r2,r8
bl printf
ldr r0,=fmt2
mov r1,r9
mov r2,r10
bl printf
mov r0, #0
ldmfd sp!,{
lr}
mov pc, lr
.end
Here we need to judge the positive and negative count of the value , Because zero is neither positive nor negative
2.6 Running results
You can see that the result is correct !

3. summary
- When operating on a given array , To put an operation into a loop , The register is addressed backwards and circularly .
- All judgment suffixes are given , stay ARM In, you can put the judgment suffix after any instruction , Used for execution after judgment ; One CMP Instructions can perform operations with many judgments .
- When compiling , Write the core code first , Then give the main function 、 Global variables 、 Add supporting conditions in the output statement .

版权声明
本文为[Im Tom Kate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210806197069.html
边栏推荐
猜你喜欢

IntelliJ更新后Kotlin项目无法运行

Comprehensive case: pinyougou project (pinyougou project process, SEO optimization, TDK three labels and codes) will be gradually optimized in the later stage

JVM——》G1垃圾收集器

Machine learning notes - SVD singular value decomposition (2)

最近发现百度云分享都要设置有提取码, 无法设置为无提取码的分享.本文将教你怎么绕过百度设置无提取码的分享(即公开的), 一行代码搞定!

综合案例:品优购项目(品优购项目流程,SEO优化,TDK三大标签,代码)后期逐步优化

关于写DMF时发生的小事情

事务的隔离级别与MVCC

36氪首发|「甄知科技」收购数智化开发平台「猪齿鱼」,将和已有产品「燕千云」融合形成产品闭环

阿里巴巴温少再度出山重构fastjson推出fastjson2
随机推荐
autojs自动化脚本怎么在电脑上开发, 详细的靠谱教程!!!
User authentication center of micro service
TX2 install ROS melody opencv3 4.5 ceres1. 14.0 Eigen3. 3.9 gtsam cv_ bridge
特征工程-特征预处理(归一化,标准化)
【ARM汇编判断】如何用汇编判断数组中正负数个数?
Explain in simple terms the optimization of ext4 block and inode distributor (Part 2)
ASUS good screen 120Hz high refresh rate, opening a new pattern of OLED Market
According to the variable name we want to visit
怎么获取png图片的创作者等信息, 不仅仅是文件大小等信息.即要怎么获取图片的元信息(metadata)
Configure multiple SSH keys
淘宝小程序体验优化:数据分析和优化实践
网络电话VOIP技术解析
Power grid enterprise standard B interface access record (II): resource reporting
IdleStateHandler 心跳检测,实现超时断开连接
Webapi (VI) - BOM
select标签 selected 选中状态动态查询
各种数据库连接字符串(EFCore)
6. 堪比JMeter的.Net压测工具 - Crank 实战篇 - 收集诊断跟踪信息与如何分析瓶颈
基于知名微服务框架go-micro开发gRPC应用程序
一定要看,MES选型九步法,值得收藏反复观看(下)