一步一步学习linux 汇编.笔记

2021-08-27  本文已影响0人  onedam

2021年8月27日 作者 幽默 :)

Assembly Language Step-by-Step

Jeff Duntemann
Colorado Springs, Colorado
June 5, 2009
www.duntemann.com/assembly.htm

Being a programmer is one thing above all else: it is understanding how
things work.

If you’re using a C compiler, you’re a lot closer
to the machine, and you see a lot more of that machinery—and must, therefore,
understand how it works to be able to use it.

However, quite a bit remains
hidden, even from the hardened C programmer.

If, conversely, you’re working in assembly language, you’re as close to the
machine as you can get.

Assembly language hides nothing, and withholds no
power.

汇编语言没有隐藏任何东西,也没有保留任何权力。 (还是有保留 只有cpu 内存硬件级别 连线 有开源的)

That’s a key point: My goal in creating this book is not entirely to teach
you assembly language per se. If this book has a prime directive at all, it is to
impart a certain disciplined curiosity about the machine, along with some basic
context from which you can begin to explore the machine at its very lowest
levels—that, and the confidence to give it your best shot. This is difficult stuff,
but it’s nothing you can’t master given some concentration(认真,全神贯注), patience, and the
time it requires—which, I caution, may be considerable.
In truth, what I’m really teaching you here is how to learn.

强调了 地址的重要性

  1. If you already understand x86 memory addressing, skip Chapter 4.
  2. No. Stop. Scratch that. Even if you already understand x86 memory
    addressing, read Chapter 4.
    Point 5 is there, and emphatic, for a reason: Assembly language programming is
    about memory addressing. If you don’t understand memory addressing, nothing
    else you learn in assembly will help you one lick
    k. So don’t skip Chapter 4
    no matter what else you know or think you know. Start from there, and see
    it through to the end. Load every example program, assemble each one, and
    run them all.
    Strive(努力) to understand every single line in every program. Take
    nothing on faith.

Take notes. 记笔记 反复练习 坚持

Ultimately, that’s the only way to learn
it: by yourself. The guidance you find—in friends, on the Net, in books like
this—is only guidance, and grease on the axles.

受到书本 网上 朋友的仅是指引. 需要自己不断的测试,练习. 然后熟练, 掌握, 产生或发展该项技术,让这个技术有效生长..

Computer programming is nothing more than this: you, the programmer,
write a list of steps and tests. The computer then performs each step and
test in sequence. When the list of steps has been executed, the computer
stops.

计算机编程只不过是这样:程序员,编写一个步骤和测试列表。然后,计算机执行每个步骤,并依次进行测试。当执行了步骤列表后,计算机将停止。

A computer program is a list of steps and tests, nothing more.


image.png

Another critical concept lies in the funny numbers at the left side of the
program step locations and data locations.

The little box (which is also a storage location) marked PROGRAM COUNTER
keeps the address of the next instruction to be performed.

The key to assembly language is understanding memory addresses:
汇编语言的关键是理解内存地址 (其他语言中的变量名称 本质上是地址 )
In such
languages as Pascal and BASIC, the compiler takes care of where some�thing is located—you simply have to give that something a symbolic
name, and call it by that name whenever you want to look at it or
change it. In assembly language, you must always be cognizant of
where things are in your computer’s memory. Therefore, in working
through this book, pay special attention to the concept of memory
addressing, which is nothing more than the art of specifying where some�thing is. The Game of Assembly Language is peppered with addresses
and instructions that work with addresses

另一个重要概念 2进制 16进制
This chapter exists because at the assembly-language level, your computer
does not understand numbers in our familiar base 10. Computers, in a slightly
schizoid fashion, work in base 2 and base 16—all at the same time. If you’re
willing to confine yourself to higher-level languages such as C, Basic or Pascal,
you can ignore these alien bases altogether, or perhaps treat them as an
advanced topic once you get the rest of the language down pat. Not here.
Everything in assembly language depends on your thorough understanding of
these two number bases, so before we do anything else, we’re going to learn
how to count all over again—in Martian

image.png

谢谢,,, 练习 练习 练习. 任何行业的专家 都是不断练习之后的结果.

The more you burn a gut-level understanding of hex into your
reflexes, the easier assembly language will be.

你对十六进制的理解越投入到肌肉反射中,汇编语言就越容易。

We’re laying important groundwork here. Take
it seriously now and you’ll lose less hair later on (头发将会变少,,ca :)) 哈哈哈)

You must become accustomed to thinking in
terms of such numbers as 2-32次方, which, after all, is only a trifling 4 billion in
decimal.
要习惯 2 1次 20次 30 64次方 这样的表示

Hexadecimal As Shorthand for Binary 16进制是二进制的缩写.

These are memory chips. You load numbers into the memory chips by flipping
these toggle switches in different binary code patterns, where ‘‘up’’ means a
1-bit, and ‘‘down’’ means a 0-bit. Each number in memory means something
to the CPU chip. One number makes it add; another number makes it subtract;
another makes it write different numbers into memory, and lots of other things.
A program consists of a bunch of these instruction-numbers in a row in memory.
The computer reads the first number, does what the number tells it to do, and
then reads the second one, does what that number says to do, and so on until it
runs out of numbers.
image.png

非常精彩!

I wrote this book in large part because I could not find a beginning text on
assembly language that I respected in the least. Nearly all books on assembly
start by introducing the concept of an instruction set, and then begin describing
machine instructions, one by one. This is moronic, and the authors of such
books should be hung. Even if you’ve learned every single instruction in an
instruction set, you haven’t learned assembly language.
You haven’t even come close.

16位地址线 可以寻址64k 66536个byte
Sixteen address lines will address 64K bytes. If you count in binary (which
computers always do) and limit yourself to 16 binary columns, you can count
from 0 to 65,535. (The colloquial ‘‘64K’’ is shorthand for the number 66,536.)
This means that every one of 65,536 separate emory locations can have its
own unique address, from 0 up to 65,535.

各司其职,各负其责, cpu 中隐藏了 管理中的组织架构精华. 设职 , 兼职. 灵活 安全 稳定. good

General-Purpose Registers
The segment registers exist only to hold segment addresses. T

As you’ll see later, in protected mode all the segment
registers ‘‘belong’’ to the operating system and are not changeable by ordinary
programs.

在保护模式下,所有的段寄存器“属于”属于操作系统,并且不能被普通程序更改(因为通过段选择子来控制权限)

The segment registers are still functioning, of course, from
the CPU’s point of view. They don’t disappear and are still there, but the
operating system sets them to values of its own choosing when it launches
your program, and those values will be good as long as your program runs.
You don’t have to access the segment registers in any way to write your
program.

当然,从CPU的角度来看,段寄存器仍然在工作。它们不会消失,而且仍然在那里,但操作系统在启动程序时将它们设置为自己选择的值,只要程序运行,这些值就会保持良好。您不必以任何方式访问段寄存器来写入程序

Segment registers are set by the operating system,
and you don't fool with them!

You the programmer do
not change code segments
directly. “Long jump”
instructions alter CS as
needed.
修改cs的是 远跳转
数据段 有 ds es ..等多个. 但cs 只有一个.
. However, you only have one code
segment register, CS. CS always points to the current code segment, and the
next instruction to be executed is pointed to by the IP register. You don’t load
values directly into CS to change from one code segment to another. Machine
instructions called jumps change to another code segment as necessary. Your
program can span several code segments, and when a jump instruction (of
which there are several kinds) needs to take execution into a different code
segment, it changes the value in CS for you

The segment registers still exist, but they work in a radically different way.
Not only don’t you have to fool with them; you can’t. The segment registers
are now considered part of the operating system, and in almost all cases
you can neither read nor change them directly.

看这本书 要先看 先练习 c 汇编 操作系统... 反复看 互相联系看.. 感恩作者.
Note that we haven’t really talked about machine instructions in detail yet,
and we’ve been able to pretty crisply define the universe in which machine
instructions exist and work.
Memory addressing and registers are key in this
business. If you know them, the instructions will be a snap. If you don’t know
them, the instructions won’t do you any good!

如果你知道了 内存空间布局 (宇宙) 每个具体指令将会很简单. 如果不知道. 这门很难入.

不理解 学习 操作系统. 学习 语言 容易成 无根之木. 操作系统与语言是互相递归的. 操作系统的规则 ,理论 概念是 语言的基石. 是语言深入的前提. 当然 只需要当司机 就行的 crud 程序员. 不必花大精力 这样深入.
What difficulty exists in programming for protected mode flat model lies in
understanding the operating system, its requirements, and its restrictions. This
can be a substantial amount of learning: Windows NT and Linux are major
operating systems that can take years of study to understand well. I’m going
to introduce you to protected mode assembly programming in flat model in
this book, but you’re going to have to learn the operating system on your own.
This book is only the beginning— there’s a long road out there to be walked,
and you’re barely off the curb.

That’s enough for the time being about the platform on which our code will
run. It’s time to start talking about the process of writing assembly language
programs, and the tools with which we’ll be doing it.

‘‘There are nine and sixty ways of constructing tribal lays,
And every single one of them is right!’’

不矛盾, 电梯 楼梯 , 火车轨 高速公路 航线 都要修.

The moral of the poem: Trust your totem pole. What’s true of tribal lays
is also true of programming methodologies. There are at least nine and sixty
ways of making programs, and I’ve tried most of them over the years. They’re
all different, but they all work, in that they all produce programs that can be
loaded and run—once the programmer figures out how to follow a particular
method and use the tools that go with it.

还是测试 反复练习 . ..
To be fair, modern high-level language compilers generally implement
something called in-line assembly, which allows a programmer to ‘‘take back’’
control from the compiler and ‘‘drop in’’ a sequence of machine instructions
of his or her own design. A great deal of modern assembly language work is
done this way, but it’s actually considered an advanced technique, because
you first have to understand how the compiler generates its own code before
you can ‘‘do better’’ using in-line assembly. (And don’t assume, as many do,
that you can do better than the compiler without a great deal of study and
practice!)

Taken together, a mnemonic and its operands are called an instruction


image.png

很容易搞不懂 上个月写的代码是干嘛的?

Beware ‘‘Write-Only’’ Source Code!
This is as good a time as any to point out a serious problem with assembly
language. The instructions themselves are almost vanishingly terse, and doing
anything useful takes a lot of them. In addition, whereas each instruction states
what it does, there is nothing to indicate a context within which that instruction
operates. You can build that context into your Pascal or Basic code with some
skill and discipline (along with identifiers that point to their purpose), but in
assembly language you can add context only through comments.
Without context, assembly language starts to turn into what is called
‘‘write-only’’ code. It can happen like this: on November 1, in the heat of
creation, you crank out about 300 instructions in a short utility program that
does something important. You go back on January 1 to add a feature to
the program—and discover that you no longer remember how it works. The
individual instructions are all correct, and the program assembles and runs
as it should, but knowledge of how it all came together and how it works
from a height have vanished under the weight of Christmas memories and
eight weeks of doing other things. In other words, you wrote it, but you can no
longer read it, or change it. Voila! Write-only code.

链接器 是个大管家 , 把大家的 所有数据收集起来. 重新分配 安排 . 事无巨细, 粗中有细, 高手..

To process several object modules into a single executable module, the linker
must first build an index called a symbol table, with an entry for every named
item in every object module it links, with information about what name (called
a symbol) refers to what location within the module. Once the symbol table is
complete, the linker builds an image of how the executable program will be
arranged in memory when the operating system loads it. This image is then
written to disk as the executable file.
The most important thing about the image that the linker builds relates
to addresses. Object modules are allowed to refer to symbols in other object
modules. During assembly, these external references are left as holes to be filled
later—naturally enough, because the module in which these external symbols
exist may not have been assembled or even written yet. As the linker builds
an image of the eventual executable program file, it learns where all of the
symbols are located within the image, and thus can drop real addresses into
all of the external reference holes.

Archimedes, the primordial engineer, had a favorite saying: ‘‘Give me a lever
long enough, and a place to stand, and I will move the Earth.’’ The old guy was
not much given to metaphor, and was speaking literally about the mechanical
advantage of really long levers, but behind his words there is a larger truth
about work in general: to get something done, you need a place to work, with
access to tools.

第六章 主要讲 编辑器 ide (过时的, 可以跳过) 187页 讲make 的可以看看

A makefile is such a blueprint. makefile 文件是工程的蓝图.

Not everything goes, of course. Whatever is inside the brackets is called the
effective address of a data item in memory, and there are rules dictating what
can be a valid effective address and what cannot. At the current evolution of
the x86 hardware, two registers may be added together to form the effective
address, but not three or more. In other words, the following are not legal
effective address forms:
mov eax,[ebx+ecx+edx]
mov eax,[ebx+ecx+esi+edi]
The more complicated forms of effective addresses are easier to demonstrate
than explain, but we have to cover a few other things first. They’re especially
useful when you’re dealing with lookup tables, which I’ll go into later. For
now, the most important thing to do is not confuse a data item with where it
exists!
Confusing Data and Its Address

变量名就是地址
In assembly language, variable names represent addresses, not data!

上一篇下一篇

猜你喜欢

热点阅读