没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
本书详细介绍了ARM 64位汇编语言以及结构化编程的概念和应用。在早期计算机时代,由于缺乏高级编程语言和编译器,几乎所有的编程工作都是通过汇编语言完成的。随着FORTRAN语言的出现,开启了结构化编程的新范式,强调代码块的线性执行和高度结构化的流程控制语句。结构化程序定理指出,程序可以通过顺序、选择和迭代三种基本结构组合来表达任何可计算的函数。尽管高级语言提供了额外的流程控制语句,但它们只是语法糖,而结构化编程的核心概念包括良好的函数和过程设计、参数传递机制、单独编译和信息隐藏等。遵循结构化编程原则的汇编语言程序更易于编写、理解、调试和维护,而且能提高代码的数据处理密度和效率。本书还探讨了如何在AArch64汇编语言中实现选择结构,包括if-then、if-then-else语句以及更复杂的条件选择,展示了使用分支指令和条件选择指令的不同方法。
资源推荐
资源详情
资源评论





格式:pdf 资源大小:339.1KB 页数:11

格式:pdf 资源大小:8.8MB 页数:118















格式:edz 资源大小:548.1MB



格式:zip 资源大小:58.6KB






CHAPTER 5
Structured programming
Before IBM released FORTRAN in 1957, almost all programming was done in assembly
language. Part of the reason for this is that nobody knew how to design a good high-level lan-
guage, nor did they know how to write a compiler to generate efficient code. Early attempts at
high-level languages resulted in languages that were not well structured, difficult to read, and
difficult to debug. The first release of FORTRAN was not a particularly elegant language by
today’s standards, but it did generate efficient machine code.
In the 1960s, a new paradigm for designing high-level languages emerged. This new paradigm
emphasized grouping program statements into blocks of code that execute from beginning
to end. These basic blocks have only one entry point and one exit point. Control of which
basic blocks are executed, and in what order, is accomplished with highly structured flow con-
trol statements. The structured program theorem provides the theoretical basis of structured
programming. It states that there are three ways of combining basic blocks: sequencing, selec-
tion, and iteration. These three mechanisms are sufficient to express any computable function.
It has been proven that all programs can be written using only basic blocks, the pre-test loop,
and if-then-else structure. Although most high level languages provide additional flow con-
trol statements for the convenience of the programmer, they are just “syntactical sugar.” Other
structured programming concepts include well-formed functions and procedures, pass-by-
reference and pass-by-value, separate compilation, and information hiding.
These structured programming languages enabled programmers to become much more pro-
ductive. Well-written programs that adhere to structured programming principles are much
easier to write, understand, debug, and maintain. Most successful high-level languages are de-
signed to enforce, or at least facilitate, good programming techniques. This is not generally
true for assembly language. The burden of writing well-structured code lies with the program-
mer, and not the language.
The best assembly programmers rely heavily on structured programming concepts. Failure to
do so results in code that contains unnecessary branch instructions and, in the worst cases, re-
sults in something called spaghetti code. Consider a code listing where a line has been drawn
from each branch instruction to its destination. If the result looks like someone spilled a plate
of spaghetti on the page, then the listing is spaghetti code. If a program is spaghetti code, then
the flow of control is difficult to follow. Spaghetti code is much more likely to have bugs, and
ARM 64-Bit Assembly Language
https://doi.org/10.1016/B978-0-12-819221-4.00012-2
Copyright © 2020 Elsevier Inc. All rights reserved.
113

CHAPTER 5
Structured programming
在1957年IBM发布FORTRAN之前,几乎所有的编程都是用汇编语言完成的。部分原因
在于当时没有人知道如何设计一个好的高级语言,也不知道如何编写一个能够生成高
效代码的编译器。早期的高级语言尝试导致了结构不清晰、难以阅读和调试的语言。F
ORTRAN的首次发布按照今天的标准来看并不是一种特别优雅的语言,但它 did 生成
高效的机器代码。
在1960年代,出现了一种设计高级语言的新范式。这种新范式强调将程序语句分组到
从头到尾执行的代码块中。这些basic blocks只有一个入口点和一个出口点。通过高度
结构化的flow con- trol语句来控制哪些基本块被执行,以及执行的顺序。结构化程序定
理提供了结构化编程的理论基础。它指出,组合基本块有三种方式:顺序、选择和迭
代。这三种机制足以表达任何可计算函数。已经证明,所有程序都可以仅使用基本块
、pre-test loop和if-then-else结构来编写。尽管大多数高级语言为程序员的便利提供了额
外的流程控制语句,但它们只是“语法糖”。其他结构化编程概念包括结构良好的函
数和过程、按引用传递和按值传递、单独编译和信息隐藏。
这些结构化编程语言使程序员的生产力大大提高。遵循结构化编程原则编写良好的程
序更容易编写、理解、调试和维护。大多数成功的高级语言都旨在强制执行或至少促
进良好的编程技术。对于汇编语言来说,这通常不是真的。编写结构良好的代码的责
任在于程序员,而不是语言。
最好的汇编程序员严重依赖结构化编程概念。不这样做会导致代码中包含不必要的分
支指令,在最坏的情况下,会导致所谓的spaghetti code。考虑一个代码列表,其中从
每个分支指令到其目的地都画了一条线。如果结果看起来像是有人在页面上洒了一盘
意大利面,那么这个列表就是意大利面代码。如果一个程序是意大利面代码,那么控
制流就很难跟踪。意大利面代码更容易出现错误,
ARM 64-Bit Assembly Language
https://doi.org/10.1016/B978-0-12-819221-4.00012-2
Copyright © 2020 Elsevier Inc. All rights reserved.
113

114 Chapter 5
Listing 5.1 if statement in C.
1
.
.
.
2 if (x >= y)
3 {
4
.
.
.
// if statement body
5 }
6
.
.
.
is extremely difficult to debug. If the flow of control is too complex for the programmer to fol-
low, then it cannot be adequately debugged. It is the responsibility of the assembly language
programmer to write code that uses a block-structured approach.
Adherence to structured programming principles results in code that has a much higher prob-
ability of working correctly. Also, well-written code has f ewer branch statements. Therefore,
the ratio of data processing statements versus branch statements is higher. High data process-
ing density results in higher throughput of data. In other words, writing code in a structured
manner leads to higher efficiency.
5.1 Sequencing
Sequencing simply means executing statements (or instructions) in a linear sequence. When
statement n is completed, statement n + 1 will be executed next. Uninterrupted sequences of
statements form basic blocks. Basic blocks have exactly one entry point and one exit point.
Flow control is used to select which basic block should be executed next.
5.2 Selection
The first control structure that we will examine is the basic selection construct. It is called
selection because it selects one of two (or possibly more) blocks of code to execute, based
on some condition. In its most general form, the condition could be computed in a variety
of ways, but most commonly it is the result of some comparison operation or the result of
evaluating a Boolean expression.
5.2.1 If-then statement
The most important form of selection is the if statement. Listing 5.1 demonstrates a simple
if statement in C. Listing 5.2 shows the AArch64 assembly translation. The first label is for

114 Chapter 5
代码清单 5.1 C语言中的if语句。
1
.
.
.
2 if (x >= y)
3 {
4
.
.
.
// if sta tement body
5 }
6
.
.
.
极其难以调试。如果控制流程对程序员来说太复杂而无法跟踪,那么它就无法被充分
调试。编写采用块结构化方法的代码是汇编语言程序员的责任。
遵循结构化编程原则的代码具有更高的正确运行概率。此外,编写良好的代码具有较
少的分支语句。因此,数据处理语句与分支语句的比例更高。高数据处理密度导致数
据吞吐量更高。换句话说,以结构化的方式编写代码会导致更高的效率。
5.1 Sequencing
排序简单来说就是按线性序列执行语句(或指令)。当语句 n 完成后,接下来将执行
语句 n +。连续的语句序列形成基本块。基本块恰好有一个入口点和一个出口点。流
控制用于选择下一个应执行哪个基本块。
5.2 Selection
我们将要检查的第一个控制结构是基本的selection构造。它被称为选择结构,因为
它基于某个条件选择执行两个(或可能更多)代码块中的一个。在其最通用的形式
中,条件可以通过多种方式计算,但最常见的是它是某些比较操作的结果或评估布
尔表达式的结果。
5.2.1 If-then statement
选择的最重要形式是if语句。清单5.1展示了C语言中一个简单的if语句。清单5.2显示
了AArch64汇编语言的翻译。第一个标签是用于

Structured programming 115
Listing 5.2 if statement in AArch64 assembly.
1
.
.
.
2 if: cmp x0, x1 // perform test
3 blt endif // if (x < y) goto endif
4
.
.
.
// if statement body
5 endif:
6
.
.
.
clarity, to show where the if statement begins, and the second label, endif: is the end of the
if statement. The condition in assembly is reversed from the C code. The C code checks if x
>= y to know if the if statement should be taken. In AArch64 assembly, the opposite check is
used with the instruction blt.Ifx<y, then the if statement is not executed. Instead, control
branches past the if statement.
5.2.2 If-then-else statement
Most high-level languages provide an extension to the basic if statement, which allows the
programmer to select one of two possible blocks of code to execute. The same behavior can
be achieved by using two sequential if statements, with logically opposite tests. However
the if-then-else construct results in code that is more efficient as well as easier to read,
understand, and debug.
5.2.2.1 Using branch instructions
Listing 5.3 shows a typical if-then-else statement in C. Listing 5.4 shows the AArch64
code equivalent, using branch instructions. Note that this method requires a conditional
branch, an unconditional branch, and two labels. This is the most common method of writ-
ing the bodies of the then and else selection constructs.
5.2.2.2 Using conditional selection
Branching is not the only way to write an if statement. AArch64 has selection instructions
that are able to choose between two source registers based on a condition code. These instruc-
tions can also optionally increment, set, or negate one of the choices.
Listing 5.5 shows the AArch64 code equivalent to Listing 5.3, using conditional operations.
Section 4.2.10 covers the eleven conditional selection instructions in AArch64. While they
cannot always be used to replace branch-based if statements, in certain examples such as
Listing 5.5, they can be used to accomplish the same task without using any branch instruc-
tions. In this and similar cases, the code is more efficient than using a branch.
剩余81页未读,继续阅读
资源评论


毛心宇
- 粉丝: 2374
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 计算机网络技术的应用及安全防御关键研究.docx
- IBMCloudBurst云基础架构概述-云概念.docx
- 基于蚁群算法的RBF神经网络在冲量式谷物流量传感器中的应用.docx
- 《特种文献数据库》使用方法(论文资料).ppt
- 李俊杰--网络互连与实现-计算机科学与技术.doc
- 当代大学生网络安全教育对策研究.docx
- 项目管理真题精选讲解.doc
- 工程项目管理概述.ppt
- 高软件工程项目师简历Word模板.doc
- 最新网络安全信息ppt通用模板.pptx
- 经典机器学习算法的极简式代码实现方案 经典机器学习算法极简实现的完整呈现 面向经典机器学习算法的极简实现方法 经典机器学习算法极简风格实现教程 经典机器学习算法的极简化实现路径 经典机器学习算法极简实
- (源码)基于AVR单片机的红外遥控车辆控制系统.zip
- 基于互联网环境下的高校辅导员思政教育工作研究.docx
- 基于物联网的开放实验室管理系统设计.docx
- 嵌入式系统软件仿真器研究分析方案与实现.doc
- 关于机械工程项目管理的讨论.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
