端午节到了,👨👩带我和妹妹一起去朱家尖。到了以后,我们在楼下的🍴里吃饭。吃完饭我们在酒店里睡觉。睡觉起来以后,我们一起到🏖️堆起了沙堡。第一步我先画好了我们的地盘。第二步,我们挖出了一条护城河。👨拿着大铲子,挖出了很深的一个坑。我想到了一个好主意,我把👨挖出来的土,都放在地盘的边缘上。第三步,我叫妹妹拿着🪣,打好多水倒进护城河里,我叫妈妈把这些泥土弄结实。而我把所有的泥土都弄到边线上。

后来我拿着大耙子,画出了一条护城河的边缘,然后我拿了一个小🔧,帮👨一起挖护城河。挖了一半,我感觉水不太够了。我和妹妹赶紧跑到海边,拿小🪣灌了一大桶水。我们赶紧跑回来,把水洒在护城河里。然后我赶紧拿起小工具继续挖呀挖。我们挖呀挖,挖呀挖,堆呀堆,堆呀堆,堆好了大半个沙堡。水又不够了,我和妹妹赶紧跑到海边,拿起水桶,刚准备灌水,忽然一阵🌊飘来,我和妹妹一起把水都给接了过来。我和妹妹大喊起来:“爸爸妈妈,马上就要涨潮啦!”我和妹妹急忙提起🪣,跑了回去。我们几个赶紧搭呀搭,搭呀搭,快速地搭了起来。只剩最后一点了,我们几个飞快地搭了起来。终于搭完了,只剩最后一个,就是挖护城河。我和妹妹赶紧来到海边,拿起🪣,这是又有一阵🌊飘来了。我和妹妹跑了回来,刚好👨👩挖好了护城河。我和妹妹把水都洒了进去。天色已经差不多了,我们也该吃饭了。我们到了🏨楼下,开始吃饭。吃完饭,我们准备去🚶‍♀️。

沙堡

🚶‍♀️的时候,我们走到了隔壁一家🏨里,到了那边,我看见了三只🐱。第一只🐱叫做“🍮”,第二只🐱叫做“咪咪”,第三只🐱叫做“小小”。那三只🐱真的好可爱。那三只🐱颜色各不相同。🍮的颜色是黑白相间的,咪咪的颜色是灰白色的,小小的颜色是跟🍮一样,是黑白相间的。🍮它喜欢吃猫粮和鱼条。咪咪喜欢吃狗尾巴草。小小它喜欢吃猫粮。我们跟🐱拜拜了以后就去🏨睡觉了。

咪咪

第二天,我们先到楼下吃了早饭。早饭非常丰富,有🍜,炒粉,粽子,还有好多好多好吃的。吃完早饭,我们去看了🐱。只看到了咪咪,剩下的🐱全没有看到。我们给咪咪喂了好多好多的草。后来我在那边知道,咪咪抓过很多很多的🐭。因为没有看到其他的🐱,所以👩带我们到海边去挖沙了。👩在旁边的🪨上,抓到了一只小🦀️。我和妹妹把沙子放在磨具里,做出了好多个冰淇凌。我们还在旁边堆了个小山,也就是沙子山。

到了中午,我们一起去吃完了午饭。吃完午饭,我们到🛏️准备睡觉。妹妹在🛏️唱了一首“找朋友”,找呀找呀找画人,找到一个画画人,敬个礼呀握握手,你是我滴画画人。起床了以后,我在楼下写作业,妹妹在旁边吵吵闹闹地玩。写完作业,我们一起玩,然后等雨停了,我们又来到了海边。我跟妹妹在旁边挖了一个大水坑。我和妹妹一起把水给填满,我和妹妹在水坑里蹦来蹦去,跳来跳去。

果果跳泥坑
可可跳泥坑

到了🌃,我们在旁边的店里吃了一顿晚餐。👩点了🦪,🐙,🐚,海瓜子,扇贝。我还喝了北冰洋,可乐。👨喝了🍺,杨梅酒。

我们吃完饭,回到家,躺在🛏️就睡着了。

第三天,我们刚起床,外面就哗哗哗地下了大雨,我发现了一句👩‍🏫叫我们的谚语:“有雨山戴帽,无雨半山腰”。我们准备回家了。我们告别了大海、🏖️,还有小🦀️,🐱。我们来到🚗上,高兴地回到了🏠。

真是一个快乐的端午啊!下一次🤷‍♀️👨👩还会带我们去哪里⛱️。我相信下次还会带我们去更好的地方玩!

Prepare the repo

1
2
3
4
cd ~/code
git clone https://github.com/joshnewans/serial_motor_demo.git
cd serial_motor_demo
colcon build --symlink-install

Client gui at the ubuntu22

1
2
source install/setup.bash
ros2 run serial_motor_demo gui

run at the pi

1
2
source install/setup.bash
ros2 run serial_motor_demo driver --ros-args -p serial_port:=/dev/ttyUSB0 -p baud_rate:=57600 -p loop_rate:=30 -p encoder_cpr:=1321

Build the motor

motor

Assemble the car

car v0.1
car v0.2

网络搭建

首先配置raspberry pi和ubuntu 20.04(开发机)到一个网络,建议买一个GL-MT300N-V2,然后把两台ip都配置到192.168.3.*

1
2
192.168.3.119 pi
192.168.3.120 robot

robot is the dev machine, and pi is my raspberry pi.

config /etc/netplan/50-cloud-init.yaml on dev machine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
network:
version: 2
ethernets:
renderer: networkd
enp0s5:
dhcp4: no
addresses:
- 192.168.3.120/24
routes:
- to: default
via: 192.168.3.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
optional: true

ubuntu server 20.04 connect to wifi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
network:
version: 2
ethernets:
eth0:
dhcp4: true
optional: true
wifis:
renderer: networkd
wlan0:
dhcp4: no
addresses:
- 192.168.3.119/24
routes:
- to: default
via: 192.168.3.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
access-points:
"gl":
password: "4da86c9782081b654f1cca03590bebf374da6084aad4c2e471aa99cdb33a144f"
optional: true

树莓派(Raspberry Pi) 设置静态IP/连接Wi-Fi

安装ros2相关工具

目前humble有点问题,所以装foxy,可以的镜像是ubuntu-20.04.5-preinstalled-server-armhf+raspi.img.xz

初始化项目

登录开发机。

1
2
3
4
5
mkdir ~/code/dev_ws/src -p
cd ~/code/dev_ws/src
git clone git@github.com:joshnewans/my_bot.git
cd ..
colcon build --symlink-install

launch

1
2
3
cd ~/code/dev_ws/
source install/setup.bash
ros2 launch my_bot rsp.launch.py

今天我们去湖州玩,我们一👀,哇是农村。一进酒店我和妹妹就开心地在楼下玩了起来。玩了一会,我们就去吃中饭。我尝了一口饭,好美味啊。吃完了午饭,我们就去外公、外婆的房间里去👀了👀。看完了,我该回自己房间去了,妹妹在外公外婆的房间里睡觉了。

我和👨👩一起去了一个地方,那边有一个小咖啡馆,然后👨👩就去买了两杯咖啡,而我却买了一根棒冰吃了起来。我坐在一个位置上👀起了书,👨也👀起了书,👩👀起了电视剧。我把书看完了,👨给我了一张纸,我开始画起了画。我画的画就是一个参加下午茶话会的人,我画完了妈妈就带我去旁边的一个地方逛了逛。回到酒店,就开始了茶话会。

👨👩,外公、外婆在打牌,而我和妹妹在沙发上玩管宝宝的游戏。玩好我们就去吃晚饭。到了睡觉时间,妹妹哭了很久,因为她想要外公来陪她睡觉。过了一会她就不哭了,因为看了小🐷佩奇。

第二天早上,我们吃完了早饭,妈妈提议,我们今天去🧗‍♀️吧。我高兴地说,好。于是我们就去🧗‍♀️了。⛰️上有好多好多的竹子。我和👩一路上都在剥竹皮。爬到一半,妹妹先和外公外婆下山了。而我、👨👩却一直往上爬。我们爬到了“点兵台”,那里有一个亭子,我们坐在里面休息了一会。来了一个🧍‍♂️和🧍‍♀️。👩和他们聊了一会天,那个🧍‍♀️说她在上面看到了一条🐍,被🐍吓到了,所以就没有继续往上爬,就下来来到了“点兵台”。🧍‍♀️拿了一根棒子,她把那根棒子给我,说这个棒子可以用来当拐杖。我也被🐍吓到了,所以赶紧拉着爸爸妈妈往下跑。

我们刚回到酒店,就吃了中饭。吃完中饭,就回去休息了一会。妹妹起床了,我和妹妹就在楼下客厅里,找个一个休闲的榻榻米。我们就在里面玩起了妈妈宝宝的游戏。等太阳快落山的时候,我、👨👩和妹妹一起去早晨去的一个路上,那边有很多的溪水。我们找到了一个地方开始玩水。而妈妈却在旁边的桥上摘了很多很多的野草莓,准备带回去给我们吃。我们回去了,👩把野草莓给我们每人分了好多。吃完野草莓,我和妹妹到外公外婆的房间边上的大蹦床,我和妹妹玩起了一个蹦床游戏,名字叫做“🐰蹦蹦跳跑”。玩了一会就到了吃饭时间,我们吃完了晚饭,就到旁边的游客栈道散步。我摘了一朵🌻和一片银杏叶。过了一会我们就回来了,然后就睡觉去了。

第三天早上,我们还是同样吃了早饭,吃完了早饭,我和妹妹又到楼下的休闲的榻榻米上玩起了👩👶游戏。玩了一会之后,我和妹妹就去蹦蹦床了。我们还是同样玩起了蹦蹦床的游戏。那个游戏就是“🐰蹦蹦跳跑”。玩好了一会,妈妈就去退房了。可是我们退完房的时候,我和外公外婆说了一声再见,因为他们要去其他地方吃中饭,先走了。我和妹妹又到了休闲的榻榻米,玩起了👩👶的游戏。过了一会,我们也要走了,我们理好了行李,带好了自己所需的东西,上了🚗。我特别的高兴,对👩说:“妈妈,这个农村真好玩,到了暑假的时候,我还要来玩!”我跟老板娘说了一声“再见”,我们就坐着🚗走了。

这次去农村,可是玩了三天,真好玩啊!我不知道下次还会去哪里,比这个更好玩,不过我觉得下一个假期,也就是暑假了,我说过暑假的时候还要来,我感觉下次的旅途还来这里,如果是新的么,我就🤷‍♀️了。这次的旅途可真愉快,我好高兴呀,这一次是五一劳动节噢!

下面我们来构造第一个半导体应用,二极管,只需要把n-type和p-type连接起来即可,是不是很简单。只有简单的基础件,才能更好的“集成”。

depletion region1

When the p-type and the n-type materials are kept in contact with each other, the junction between them behaves differently from either side of the material alone. The electrons and holes are close to each other at the junction. According to coulomb’s law, there is a force between the negative electrons and the positive holes. When the p-n junction is formed a few electrons from the n-type diffuse through the junction and combines with the holes in the p-side to form negative ions and leaves behind positive ions in the n-side. This results in the formation of the depletion layer, which acts as the barrier and does not allow any further flow of electrons from the n region to the p region.

No Applied Bias(V = 0V)

no-bias

在没有外部电压的情况下,depletion region正常存在。n type这边主要载流子是电子,但是如果要流向p type,需要克服正电子的吸引,所以只有一小部分能够过去,这个数量和从p type流过来的电子(少数载流子)数目接近,也就是图12 c 左上的和右下的正好差不多抵消。同理两个也差不多。所以总体电流为0。

In the absence of an applied bias across a semiconductor diode, the net flow of charge in one direction is zero.

Reverse-Bias Condition()

reverse-bias

在反向电压的情况下,n type里面的电子会被“抽走”,所以原来图12 c右下的向右的会几乎为0。depletion region会加厚。p type的不会有影响,所以存在的电流就是 Minority-carrier flow.

The current that exists under reverse-bias conditions is called the reverse saturation current and is represented by .

Foward-Bias Condition()

foward-bias

当正向电压的时候,首先depletion region会变薄,因为n type这边会有大量电子注入,重新和正电子结合,p type的电子会被“抽走”。当电压加到一定程度,突然大量电子从n涌入p,图12 c 右下的是会不断增大。

  • is the reverse saturation current
  • is the applied forward-bias voltage across the diode
  • n is an ideality factor, which is a function of the operating conditions and physical construction; it has a range between 1 and 2 depending on a wide variety of factors (n =1 will be assumed throughout this text unless otherwise noted).

  • k is Boltzmann’s constant =
  • is the absolute temperature in kelvins = 273 + the temperature in
  • q is the magnitude of electronic charge =
diode-characteristics

虚线是理想的曲线,实现是实际的情况。

The actual reverse saturation current of a commercially available diode will normally be measurably larger than that appearing as the reverse saturation current in Shockley’s equation.

反向的饱和电流实际会在理想的下方,主要原因是:

  • – leakage currents

  • – generation of carriers in the depletion region

  • – higher doping levels that result in increased levels of reverse current

  • – sensitivity to the intrinsic level of carriers in the component materials by a squared

    factor—double the intrinsic level, and the contribution to the reverse current could

    increase by a factor of four.

  • – a direct relationship with the junction area—double the area of the junction, and

    the contribution to the reverse current could double. High-power devices that have

    larger junction areas typically have much higher levels of reverse current.

  • – temperature sensitivity—for every 5°C increase in current, the level of reverse sat- uration current in Eq. 1 will double, whereas a 10°C increase in current will result in

    doubling of the actual reverse current of a diode.

A semiconductor material that has been subjected to the doping process is called an extrinsic material.

There are two extrinsic materials of immeasureable importance to semiconductor device fabrication: n-type and p-type materials.

n-Type Material

n-type就是有five valence electrons(pentavalent), 比如锑,砷,磷。这多出来的电子,不和共价键关联,成为一种相对free的状态。

Diffused impurities with five valence electrons are called donor atoms.

要注意的是,虽然n-type多了很多free carriers,但是始终是中性的。因为positively charged protons in the nuclei和free and orbiting charged electrons数量相等。

n-type-material

doping之后,在原来的energy level,又加了一层,这一层的电子很容易在room temperature就可以获得能量到Conduction band,原来Si material 个atoms中有一个free电子,现在是里面有一个,是原来的100,000倍。

donor-impurities-effect

p-Type Material

同理加入three valence electrons,比如硼,镓,铟。这样共价键就缺失了一个电子。

The diffused impurities with three valence electrons are called acceptor atoms.

和n-type一样,p-type也是中性。

p-type-material

Electron versus Hole Flow

当一些电子吸收了能量,打破共价键,填充到hole中,释放电子的地方就形成了hole。看起来像是电子的流动或者hole的流动。

electron-hole-flow

Majority and Minority Carriers

一半的Ge和Si,只有少数的电子获得能量打破共价键,留下holes。n-type电子远比hole多,而p-type,hole远比电子多。其实主要是参杂的一方电子或hole急剧增多。

In an n-type material the electron is called the majority carrier and the hole the minority carrier.

In a p-type material the hole is the majority carrier and the electron is the minority carrier.

majority-minority-carriers

当donor atom的第五个电子离开parent atom,这个atom就带有正电,同理,acceptor atom有电子填充到hole,就带负电。其实这里要注意的是一种动态性,也就是电子获得外部能量后“离开”的行为,这样左图就出现了,电子比较多,所以容易获得能量而“游离”存在。同理,hole也会和电子交换,那么左图的就是由于交换得到电子而变为负,hole比较多,容易获得能量而“游离”存在。

n-type和p-type是semiconductor devices的basic building blocks,比如把它们合并在一起,就会形成p-n结,是不是很酷😊

The farther an electron is from the nucleus, the higher is the energy state, and any electron that has left its parent atom has a higher energy state than any electron in the atomic structure.

energy-gap

Only specific energy levels can exist for the electrons in the atomic structure of an isolated atom. The result is a series of gaps between allowed energy levels where carriers are not permitted.

energy-levels

There is a minimum energy level associated with electrons in the conduction band and a maximum energy level of electrons bound to the valence shell of the atom. Between the two is an energy gap that the electron in the valence band must overcome to become a free carrier. That energy gap is different for Ge, Si, and GaAs; Ge has the smallest gap and GaAs the largest gap. In total, this simply means that:

An electron in the valence band of silicon must absorb more energy than one in the valence band of germanium to become a free carrier. Similarly, an electron in the valence band of gallium arsenide must gain more energy than one in silicon or germanium to enter the conduction band.

Ge devices:

  • photodetectors sensitive to light
  • security system sensitive to heat

Si and GaAs:

  • transistor networks, stability is a high priority

The wider the energy gap, the greater is the possibility of energy being released in the form of visible (infrared) light waves. For GaAs the gap is sufficiently large to result in significant light radiation.

The units of measurement are electron volts (eV). The unit of measure is appropriate because W (energy) = QV (as derived from the defining equation for voltage: V = W/Q). Substituting the charge of one electron and a potential difference of 1 V results in an energy level referred to as one electron volt.

每个小电子都要一步一步往上爬,最终达到一个free的状态, 人生不是这样吗?

今天我们一起去hello kitty乐园,我和妹妹开心极了。在hello kitty公园的一路上,我和妹妹叽叽喳喳地开心极了。

到了hello kitty乐园,我们兴奋极了。我们刚进去看了一会表演,我和爸爸一起坐了苹果树的飞椅,我感觉超级刺激。妹妹和妈妈一起去了另一边,坐旋转木马。坐完了苹果飞椅,我们去找👩和妹妹。我们到了那里却没有看到她们。过了一会,我们才看到了她们。我和妈妈去骑魔法单车,爸爸和妹妹去排另一个项目的队。我和👩坐好了以后,就来到另一边彩球飞舞的队伍。我和妹妹一直扶着中间的东西,转啊转,转啊转,把爸爸转晕了。坐完了我说还想坐一次,爸爸说不要了,太晕了。我们去看了一个表演。那时候我和妹妹肚子好饿,我们就去找餐厅。我们找到一家,但是它关门了,我们就问了一问旁边的人,他告诉我们一共有三个餐厅,让我们选,我们选了🍎派餐厅,到了以后我和妹妹还有爸爸都饿的不得了,👩就去点餐了。我跑过去问👩,你准备点什么?👩说点一个hello kitty套餐,再点两份饭。过了一会我跑过去帮妈妈端菜。饭刚到,我和妹妹就兴奋地吃了起来。

等我们吃完饭了以后,我们就来到天使剧场排队,可是那里一个人没有,原来还要一个小时。这是突然听见了音乐的响声,我回头一看,原来是花车游行,就要开始啦!我们赶紧冲下楼梯,挤来挤去的终于挤出了一个位置,时间刚刚好,花车马上就要开始了。音乐响起了,花车游行和演员们都出来了,我和妹妹都高兴极了,每个花车前面,都有五六个人跳舞,看完了以后,我们就一下子跑到天使剧场排队,哇,好多人在排队啊!我们左等右等,上等下等,等了好久好久,我跑到前面看了一眼,我看到了天使剧场开门了。我赶紧跑回来更爸爸妈妈说,前面开门了。我们进去找到位置就坐。很快,表演就开始了。

我们看到了一个hello kitty,她介绍了自己的围裙,是时间围裙。看完了以后,我心里在想,这一个表演好像是由一会演,一会唱歌组成的。我们一出去,哇,大太阳好大啊。把我们晒的好热啊。我和妹妹赶紧跟妈妈说,我们能不能吃一下🍦,妈妈说可以的。我们的🍦是🍫味的,我和妹妹吃的很高兴。妈妈说你们吃的怎么这么快。吃完我们就去看了hello kitty小屋。看完就去礼品店买了🎁。刚一出去,我们看到有表演,我们一下子就冲了过去,挤呀挤,终于挤出了一个位置。演完我们就到了有水的项目。我在那边选了一个欢乐漂流的项目。我和妈妈到里面排队,买了一件雨衣,第一次滑下来的时候,我和妈妈的雨衣并没有淋湿。妈妈说再滑一次吧。我跟妈妈说,对啊,就应该再滑一次,不然这个雨衣还不如不用穿。你付了钱,应该把它淋湿一点才好。第二次滑下来,有一点淋湿了。

坐完我们就把雨衣丢掉,准备回家了。我们很快来到了停车场,就开车回家了。

这一次的hello kitty一日游,太有趣了,不知道下次爸爸妈妈还会带我们去哪儿,像今天一样有意思,好玩,还能买礼品的地方,我好期待呀!我特别想知道一下下次去哪儿玩。

atomic-structure

As indicated in Fig. 3, silicon has 14 orbiting electrons, germanium has 32 electrons, gallium has 31 electrons, and arsenic has 33 orbiting electrons.

For germanium and silicon there are four electrons in the outermost shell, which are referred to as valence electrons.

Valence electrons are electrons in the outer shells that are not filled. Because valence electrons have higher energy than electrons in inner orbits, they are involved in the majority of chemical processes. They assist us in determining the chemical properties of an element, such as its valency or how it forms bonds with other elements.

Gallium has three valence electrons and arsenic has five valence electrons. Atoms that have four valence electrons are called tetravalent, those with three are called trivalent, and those with five are called pentavalent.

The term valence is used to indicate that the potential (ionization potential) required to remove any one of these electrons from the atomic structure is significantly lower than that required for any other electron in the structure.

Ionization energy, also called ionization potential, is the energy necessary to remove an electron from the neutral atom. X + energy → X+ + e− where X is any atom or molecule capable of being ionized, X + is that atom or molecule with an electron removed (positive ion), and e − is the removed electron.

covalent-bonding

In a pure silicon or germanium crystal the four valence electrons of one atom form a bonding arrangement with four adjoining atoms, as shown in Fig. 4.

This bonding of atoms, strengthened by the sharing of electrons, is called covalent bonding.

Because GaAs is a compound semiconductor, there is sharing between the two different atoms, as shown in Fig. 5. Each atom, gallium or arsenic, is surrounded by atoms of the complementary type. There is still a sharing of electrons similar in structure to that of Ge and Si, but now five electrons are provided by the As atom and three by the Ga atom.

covalent-binding-gaas

Although the covalent bond will result in a stronger bond between the valence electrons and their parent atom, it is still possible for the valence electrons to absorb sufficient kinetic energy from external natural causes to break the covalent bond and assume the “free” state. The term free is applied to any electron that has separated from the fixed lattice structure and is very sensitive to any applied electric fields such as established by voltage sources or any difference in potential. The external causes include effects such as light energy in the form of photons and thermal energy (heat) from the surrounding medium. At room temperature there are approximately free carriers in of intrinsic silicon material, that is, 15,000,000,000 (15 billion) electrons in a space smaller than a small sugar cube—an enormous number.

欢迎来到电子的微观世界!

legacy

Real mode

The central processing unit(CPU) always starts in real mode, and then the main loader usually executes the code to explicitly switch it to protected mode and then to the long mode.

These are the registers usable in real mode:

  • ip, flags;
  • ax, bx, cx, dx, sp, bp, si, di;
  • Segment registers: cs, ds, ss, es, (later also gs and fs).

As it was not straightforward to address more than 64 Kilobytes of memory, engineers came up with a solution to use special segement registers in the following way:

  • Each physical address consists of 20 bits(so, 5 hexadecimal digits).

  • Each logical address consists of two components. One is taken from a segment register and encodes the segment start. The other is an offset inside this segment. The hardware calculates the physical address from these components the following way:

    physical address = segment base * 16 + offset

    You can often see addresses written in form of segment:offset, for example:

    1
    4a40:0002, ds:0001, 7bd3:ah

    Note that strictly speaking, the segment register do not hold segments' starting addresses but rather their parts(the four most significant hexadecimal digits). By adding another zero digit to multiply it by we get the real segment starting address.

    Each instruction referencing memory implicitly assumes usage of one of segment registers. Documentation clarifies the default segment registers for each instruction. However, common sense can help as well. For instance, mov is used to manipulate data, so the address is relative to the data segment.

    1
    mov al, [0004]	; === mov al, ds:04444

    It is possible to redefine the segment explicitly:

    1
    mov al, cs:[0004]

    When the program is loaded, the loader set ip, cs, ss, and sp register to that cs:ip corresponds to the entry point, and ss:sp points on top of the stack.

    Real mode has numerous drawbacks

    • It makes multitasking very hard. The same address space is shared between all programs, so they should be loaded at different addresses. Their relative placement should usually be decided during compilation. :joy: But maybe we can distributed these tasks by hand.
    • Programs can rewrite each other's code or even operating system as they all live in the same address space. :dog: What about only one user?
    • Any program can execute any instruction, including those used to set up the processor's state. Some instructions should only be used by the operating system(like those used to set up virtual memory, perform power management, etc.) as their incorrect usage can crash the whole system. :laughing: We do not have operating system!

Protected Mode

Intel 80386 was the first processor implementing protected 32-bit mode.

It provides wider versions of registers(eax, ebx, ..., esi, edi) as well as new protection mechanisms: protection rings, virtural memory, and an improved segmentation.

Obtaining a segment starting address has changed.

Linear address = segment base(taken from system table) + offset

Each of segment registers cs, ds, ss, es, gs, and fs stores so-called segment selector, containing an index in a special segment descriptor table and a little additional information.

Two types of segment descriptor tables:

  • LDT(Local Descriptor Table)
  • GDT(Global Descriptor Table)
segment-selector

Index denotes descriptor position in either GDT or LDT. The T bit select either LDT or GDT. As LDTs are no longer used, it will be zero in all cases.

The table entries in GDT/LDT also store information about which privilege level is assigned to the described segment. When a segment is accessed through segement selector, a check of Request Privilege Level(RPL) value(stored in selector = segment register) against Descriptor Privilege Level(stored in descriptor table) is performed. If RPL is not privileged enough to access a high privileged segment, an error will occur. This way we could create numerous segments with various permissions and use RPL values in segment selectors to define which of them are accessible to us right now(given our privilege level).

segment-descriptor

G-Granularity, e.g., size is in 0=bytes, 1=pages of size 4096 bytes each.

D-Default operand size(0=16 bit, 1=32 bit).

L-Is it a 64-bit mode segment?

V-Available for use by system software.

P-Present in memory right now.

S-Is it data/code (1) or is it just some system information holder (0).

X-Data (0) or code (1).

RW-For data segment, is writing allowed? (reading is always allowed); for code segment, is reading allowed? (writing is always prohibited).

DC-Growth direction: to lower or to higher addresses? (for data segment); can it be executed from higher privilege levels? (if code segment)

A-Was it accessed?

DPL-Descriptor Privilege Level (to which ring is it attached?)

Enabling Protected Mode loader_start32.asm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
lgdt cs:[_gdtr]

mov eax, cr0 ; !! Privileged instruction
or al, 1 ; this is the bit responsible for protected mode
mov cr0, eax ; !! Privileged instruction
jmp (0x1 << 3):start32 ; assign first seg selector to cs

align 16
_gdtr: ; stores GDT's last entry index + GDT address
dw 47
dq _gdt

align 16

_gdt:
; Null descriptor (should be present in any GDT)
dd 0x00, 0x00
; x32 code descriptor:
db 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00 ; differ by exec bit
; x32 data descriptor:
db 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00 ; execution off (0x92)
; size size base base base util util|size base

Accessing Parts of Registers

1
2
3
4
5
6
7
8
9
10
11
mov rax, 0x1122334455667788			; rax = 0x1122334455667788
mov eax, 0x42 ; !rax = 0x00 00 00 00 00 00 00 42
; why not rax = 0x1122334400000042 ??

mov rax, 0x1122334455667788 ; rax = 0x1122334455667788
mov ax, 0x9999 ; rax = 0x1111222233339999, as expected
; this works as expected

mov rax, 0x1122334455667788 ; rax = 0x1122334455667788
xor eax, eax ; rax = 0x0000000000000000
; why not rax = 0x1122334400000000?

As you see, writing in 8-bit or 16-bit parts leaves the rest of bits intact. Writing to 32-bit parts, however, fill the upper half of a wide register with sign bit!

Explanation

Let's think about instruction decoding. The part of a CPU called instruction decoder is constantly translating commands from an older CISC system to a more convenient RISC one. Pipelines allow for a simultaneous execution of up to six smaller instructions. To achieve that, however, the notion of registers should be virtualized. During microcode execution, the decoder choose an available register from a large bank of physical registers. As soon as the bigger instruction ends, the effects become visible to programmer: the value of some physical registers may be copied to those, currently assigned to be, let's say, rax.

The data interdependencies between instructions stall the pipeline, decreasing performance. The worst cases occur when the same register is read and modified by several consecutive instructions(think about rflags!).

If modifying eax means keeping upper bits of rax intact, it introduces an additional dependency between current instruction and whatever instruction modified rax or its parts before. By discarding upper 32 bits on each write to eax we eliminate this dependency, because we do not care anymore about previous rax value or its parts.

This kind of a new behavior was introduced with the latest general purpose registers' growth to 64 bits and does not affect operations with their smaller parts for the sake of compatibility. Otherwise, most older binaries would have stopped working because assigning to, for example, bl, would have modified the entire ebx, which was not true back when 64-bit registers had not yet been introduced.

0%