2016年1月28日星期四

小窥MIPS汇编编程

今天接触了一些MIPS编程,算是入了个门,记录一下。分为三个部分,加减乘除,布尔运算与条件语句,和栈指针的操作。


  • 加减乘除运算


1.1 加法运算4 + 6 和 乘法运算 21 * 2

.text
main:
li $t1, 4
li $t2, 6
add $a0, $t1, $t2
li $v0, 1
syscall

li $t3, 21
li $t4, 2
mul $a0, $t3, $t4
li $v0, 1
syscall

.data

mul和add的结果写进 $a0 中,然后 li $v0, 1 syscall 结束

1.2 加法与乘法混合运算 4 + 7/2

.text
li $a0, 4
li $a1, 7
li $a2, 2
div $a1, $a1, $a2
add $a0, $a1, $a1
li $v0, 1
syscall


.data


  • 布尔运算与条件语句
2.1 true && false


.text

li $t1, 1
li $t2, 0
and $a0, $t1, $t2
li $v0, 1
syscall
.data

2.2 if 3 != 4 then 10 * 2 else 14

.text
li $t1, 3
li $t2, 4
li $t3, 10
li $t4, 2
bne $t1, $t2, goto
li $a0, 14
li $v0, 1
syscall
goto:
mul $a0, $t3, $t4
li $v0, 1
syscall
.data

bne $t1, $t2, goto 若前半句bne判断为真,则执行goto语句(可以视作一个function),否则顺序执行 li $a0, 14

2.3 2 = 3 || 4 <= 2 * 3

.text
li $t1, 2
li $t2, 3
li $t3, 4
li $t4, 2
li $t5, 3
beq $t1, $t2, goto2
mul $t4, $t4, $t5
sub $t3, $t3, $t4
blez $t3, goto2
li $a0, 0
jal print_int
li $v0, 10
syscall
goto2:
li $a0, 1
jal print_int
li $v0, 10
syscall
print_int:
li $v0, 1
syscall
jr $ra

|| 为与或运算,这里我们首先判断左边。若左为真,则直接输出真;若左不为真,则判断右边。

具体的,判断 2 = 3, 我们使用 beq 指令。右边的,MIPS没有直接判断两个数字 <= 的指令,我们使用blez,blez是判断一个数是否小于等于0;所以我们先执行一个减法操作,然后blez判断差与0的关系。

这里还定义了print_int,jal跳转至print_int,li $v0, 1输出真值1,然后 jr $ra 返回 。

给定一个栈,出栈顶两个元素,对其执行加法操作,再重新把结果压进栈里。这里涉及内存地址的读写操作。

.text

main:
add $sp, $sp, -8 
li $a0, 36
li $a1, 6
sw $a0, 0($sp) 
sw $a1, 4($sp)
jal addLast
li $v0, 10
syscall
addLast:
lw $t0, 0($sp)
lw $t1, 4($sp) 
addi $sp, $sp, 8
add $t0, $t0, $t1
addi, $sp, $sp, -4 
sw $t0, 0($sp)
jr $ra
.data

初始化$sp栈顶于 -8。我们将两个数36和6写进 地址为0($sp)和4($sp)里,用sw指令。
addLast方法里,我们将如上两个地址的值读出相加得$t0, 然后栈顶$sp地址减4,$t0写进新的$sp地址里。

刚写完又发现一篇很好的MIPS入门,中文的,比翻官方文档清晰一些。链接在这里

2016年1月18日星期一

android:layout_gravity 和 android:gravity 的区别

为了item的小部件调整操碎了心,发现一篇好文章。转载之,原链接在这里

gravity 这个英文单词是重心的意思,在这里就表示停靠位置的意思。
android:layout_gravity 和 android:gravity 的区别
从名字上可以看到,android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。
android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。
比如说button: android:layout_gravity 表示按钮在界面上的位置。 android:gravity表示button上的字在button上的位置。

可选值
这两个属性可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。
而且这些属性是可以多选的,用“|”分开。
默认这个的值是:Gravity.LEFT
对这些属性的描述:
出自:
ValueDescription
topPut the object at the top of its container, not changing its size.
将对象放在其容器的顶部,不改变其大小.
bottomPut the object at the bottom of its container, not changing its size.
将对象放在其容器的底部,不改变其大小.
leftPut the object at the left edge of its container, not changing its size.
将对象放在其容器的左侧,不改变其大小.
rightPut the object at the right edge of its container, not changing its size.
将对象放在其容器的右侧,不改变其大小.
center_verticalPlace object in the vertical center of its container, not changing its size.
将对象纵向居中,不改变其大小.
垂直对齐方式:垂直方向上居中对齐。
fill_verticalGrow the vertical size of the object if needed so it completely fills its container.
必要的时候增加对象的纵向大小,以完全充满其容器.
垂直方向填充
center_horizontalPlace object in the horizontal center of its container, not changing its size.
将对象横向居中,不改变其大小.
水平对齐方式:水平方向上居中对齐
fill_horizontalGrow the horizontal size of the object if needed so it completely fills its container.
必要的时候增加对象的横向大小,以完全充满其容器.
水平方向填充
centerPlace the object in the center of its container in both the vertical and horizontal axis, not changing its size.
将对象横纵居中,不改变其大小.
fillGrow the horizontal and vertical size of the object if needed so it completely fills its container. This is the default.
必要的时候增加对象的横纵向大小,以完全充满其容器.
clip_verticalAdditional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip is based on the vertical gravity: a top gravity clips the bottom edge, a bottom gravity clips the top edge, and neither clips both edges.
附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.
垂直方向裁剪
clip_horizontalAdditional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip is based on the horizontal gravity: a left gravity clips the right edge, a right gravity clips the left edge, and neither clips both edges.
附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.
水平方向裁剪
简单记忆 : horizontal 都是操作的水平方向,即横向, vertical 都是炒作的垂直方向,即纵向。
对于LinearLayout何时生效的问题
参看:也谈layout_gravity和gravity
http://www.lephone.net/viewthread.php?tid=325
对于 LinearLayout
当 android:orientation="vertical"  时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。
当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

2016年1月16日星期六

搞定emacs主题配色

我的emacs插件管理做的不是太好,一直都比较混乱,今天逢周六,好好地整理了一番,顺道把早就想弄的emacs主题给解决了,这里特指Linux平台下的emacs

先铺垫些前瞻知识。emacs的插件功能很强大,很适合爱折腾的人,自有配置。

1. 所有的配置信息都需要用lisp方言写入.emacs文件内,默认是在~/目录下。

2.所有的插件都需要在.emacs里导入,形如require 'name-of-tools。当然首先你要告诉emacs你的插件目录在哪里,即 add-to-list 'load-path "address"。为了方便管理和备份,强烈建议固定插件目录,一般大家使用~/.emacs.d目录。

好了,话归正题,我们要配置emacs的主题,不伤眼的舒服的主题!

1. 首先现在主题包地址,戳这里,目前版本是6.6.0

2. 得到压缩包,我们只需要压缩包里的themes文件夹和color-theme.el两个文件,cp命令把其拷进~/.emacs.d目录,cp -r themes/ ~/.emacs.d和cp color-theme.el ~/.emacs.d

3. 在根目录下打开.emacs配置文件,我们需要完成配置信息,简简单单只有四行,

;;themes
(add-to-list 'load-path "~/.emacs.d")  ;;load path 
(require 'color-theme)  ;;载入theme插件
(color-theme-initialize) ;;启动时初始化theme
(color-theme-calm-forest) ;;选择具体的主题

4. 这里我随便载入了一个名为color-theme-calm-forest的主题。其实我们还有更多的选择。打开emacs后,M-x color-theme-select 回车,就会列出当前theme包里的所有主题来,共计92项。大家可以按需选择,然后重新更改配置文件即可。例如,我们相上了TTY Dark这个主题,那么最后一行改为(color-theme-tty-dark)即可(这个更好看 -.-)

5. 除了这个标准的Theme包,网上还有其他的选择,配置思路同上。

最终效果如图



****************Wed 28 Dec, 2016**************
配置 Mac 下的 Emacs更新下

发现 Emacs24以后的版本都拥有比较强大的 主题管理功能,

M-x customize-themes

会看到如图所示的预置主题列表,适合心累了不折腾的同学,选择一个保存即可。


2016年1月13日星期三

解决笔记本Ubuntu发热问题(CPU/显卡)

之前用Fedora并无太大感受,现在用Ubuntu时不时地温度飙升(KDE桌面下会好些),Psensor显示的Physical id o,Core 0 和 Core 1仨的温度,经常不合时宜地到60+,max竟然能达到三位数,掌托发热严重影响敲代码。一番探索,找到了解决方案,现在温度基本稳定在40~50间,掌托无只管发热感受了。

1. 想查看设备温度请安装Psensor,安装如下,

sudo apt-add-repository ppa:jfi/ppa
sudo apt-get update
sudo apt-get install psensor

2. 发热的主要是三个地方,Physical id o,Core 0 和 Core 1,很明显,分别是独显和CPU。我的笔记本是蛋碎的A卡+Inter双显卡方案,在windows上一只是关闭独显只用集显的,笔记本就清爽了很多很多。同理,在ubuntu上我也想这样。

sudo gedit /etc/rc.local

默认只有这一行,

echo OFF > /sys/kernel/debug/vgaswitcheroo/switch

意思是关闭不使用的可切换显卡设备。问题是ubuntu默认开启的八成是我的独显,老子只要集显!!那么这样修改好了,

echo IGD > /sys/kernel/debug/vgaswitcheroo/switch
echo OFF > /sys/kernel/debug/vgaswitcheroo/switch

添加的第一行是启用了集显,最后的那行exit 0不要动。in this way, you can turn off your 独显。~~

3. 如上,够吗,不够。我先让独显也清爽爽!研究下ubuntu的独显驱动,位置System Settings -> Software&Updates -> Addition Drivers。默认的A卡驱动用的是首选项,后缀是(open source, tested),我这儿还显示有两个后缀是(proprietary)的可选项。孰优孰劣呢?我在askubuntu论坛找到了一个比较靠谱的说法,如下,

Most of the open source drivers you'll find may produce better graphical output than the proprietary do. Sometimes you won't even notice a difference. And sometimes an open source driver works much worse than the proprietary one. I will not make a general recommendation on which to use, but here are some cases, in which certain drivers are better than others:
  • If you have a nVidia card with Optimus, you should install the open source driver from theBumblebee Project. This is the only driver supporting Optimus on Linux. you should either install the open source driver from the Bumblebee Project, or any nVidia proprietary driver, but not the Xorg driver, as it currently doesn't support discrete graphics.
  • If you want to use CUDA (nVidia's stuff for executing functions on the GPU), you should use the proprietary driver.
  • If you have a AMD APU (Accelerated Processing Unit), you should install the proprietary driver fglrx, even if you don't use the embedded graphics chip. Else, the APU would stay at its lowest frequency.
  • If you want to use OpenCL (something like CUDA, developped by Khronos), you have to use the proprietary driver.
If none of these applies to you, it's up to you which driver you use. If you don't have any problems with the current driver, I wouldn't change it. You never know whether another one will even work at all. However, if you want to take the risk, try the drivers suggested in Software & Updates→ Additional Drivers and find out which one works best.

大意是,n卡要使用CUDA建议使用proprietary(私有)驱动,A卡建议使用fglrx proprietary私有驱动....娘希匹,就得用proprietary驱动?

我的A卡是陈旧的6630,查询到这段消息之前,我盲选的第二个fglrx-updates proprietary,效果很棒。个人的建议是,首先上官网,A卡官网的驱动就是这个fglrx proprietary。官方网站有对应的最好,没有的话各位可以挨个尝试...

4. 末了,BIOS之类的CPU类似cool and quiet选项各位都可以选上,等等。我的Ubuntu版本是14.04 LTS,如上解决方案对近几年的版本有普适性,别问我why -.-

2016年1月7日星期四

解决Ubuntu下gedit打开txt文件乱码问题

这是一个常见问题,默认用Gedit打开windows分区下的或拷贝来的.txt文件,会遇到乱码问题。究其原因是,两个系统的编码方式不一。Linux用的是UTF-8,而windows下的txt则是GBK,孰优孰劣不做判定,怎么解决问题是关键。

很简单呀,更改编码方式即可呀!可是找了半天Gedit也没有修改encodings的选项,这时候就需要dconf-Editor了。

1. sudo apt-get install dconf-tools

2. 打开dconf-editor

3. 定位org -> gnome -> gedit -> preferences -> encodings, 如图



在auto-detected 中添加 ‘GB18030’ 在‘UTF-8’之后
在shown-in-menu 中添加‘GB18030’, 回车生效。

再次打开,问题解决。

这是一个gedit一劳永逸的解决方案,如果不想大动干戈,可以选择乱码文件另存为,另存为选项中记得勾上如上GBK的编码方式,新文件也可避免乱码的问题。

下面我疑惑的是,怎么更改系统的编码,使其能够兼容GBK,难道要每一个编辑软件都要分别设定encodings吗?虽然之前的emacs和sublime也是分别设置的。

解决方法如下,

1. 修改/var/lib/locales/supported.d/local文件, 添加如下两行,记得用sudo修改
zh_CN.GBK GBK
zh_CN.GB2312 GB2312

2.sudo dpkg-reconfigure --force locales

这样, Ubuntu就支持GBK编码了