《Programming from the Ground Up》阅读笔记:p103-p116

news/2024/9/18 8:55:04/文章来源:https://www.cnblogs.com/codists/p/18378059

《Programming from the Ground Up》学习第7天,p103-p116总结,总计14页。

一、技术总结

1.读写文件

(1)linux.s

linux.s:

#file name:linux.s# system call numbers(按数字大小排列,方便查看)
.equ SYS_READ, 0
.equ SYS_WRITE, 1
.equ SYS_OPEN, 2
.equ SYS_CLOSE, 3
.equ SYS_EXIT, 60# standard file descriptors
.equ STDIN, 0
.equ STDOUT, 1
.equ STDERR, 2# common status codes
.equ END_OF_FILE, 0

(2)record-def.s

record-def.s:

#file name: record-def.s.equ RECORD_FIRSTNAME, 0
.equ RECORD_LASTNAME, 40
.equ RECORD_ADDRESS, 80
.equ RECORD_AGE, 320
.equ RECORD_SIZE, 328

(3)read-record.s & read-record.s

read-record.s:

#file name: read-record.s.include "record-def.s"
.include "linux.s"# stack local variables
.equ ST_READ_BUFFER, 16
.equ ST_FILEDES, 24.section .text.global read_record
.type read_record, @function
read_record:push %rbpmov %rsp, %rbppush %rbxmov ST_FILEDES(%rbp), %rdimov ST_READ_BUFFER(%rbp), %rsimov $RECORD_SIZE, %rdxmov $SYS_READ, %raxsyscallpop %rbxmov %rbp, %rsppop %rbpret

read-records.s:

#file name: read-records.s.include "linux.s"
.include "record-def.s".section .datafilename:.ascii "ch6/test.dat\0"newline:.ascii "\n\0".section .bss.lcomm RECORD_BUFFER, RECORD_SIZE.section .text.global _start_start:.equ INPUT_DESCRIPTOR, -8.equ OUTPUT_DESCRIPTOR, -16mov %rsp, %rbp# open ch6/test.datmov $SYS_OPEN, %raxmov $filename, %rdimov $0, %rsimov $0666, %rdxsyscallpush %rax       # push input file descriptor onto stackpush $STDOUT    # push output file descriptor onto stackrecord_read_loop:# invoke read_record functionpush INPUT_DESCRIPTOR(%rbp)push $RECORD_BUFFERcall read_recordadd $16, %rsp       # pop function args off of stackcmp $RECORD_SIZE, %raxjne finished_readingpush $RECORD_FIRSTNAME + RECORD_BUFFERcall count_charsadd $8, %rsp        mov %rax, %rdx                      # count of chars to printmov $RECORD_BUFFER, %rsimov OUTPUT_DESCRIPTOR(%rbp), %rdimov $SYS_WRITE, %raxsyscallmov $1, %rdx                      # count of chars to printmov $newline, %rsimov OUTPUT_DESCRIPTOR(%rbp), %rdimov $SYS_WRITE, %raxsyscalljmp record_read_loopfinished_reading:mov $SYS_EXIT, %raxmov $0, %rdisyscall

(4)write-record.s & write-records.s

write-record.s:

#filename:write-record.s.include "linux.s"
.include "record-def.s"#PURPOSE:   This function writes a record to
#           the given file descriptor
#
#INPUT:     The file descriptor(%rdi) and a buffer(%rsi)
#
#OUTPUT:    This function produces a status code
#
.section .text.globl write_record.type write_record, @functionwrite_record:#将 system call number 1存入rax寄存器,执行syscall的时候表示执行write操作movq  $SYS_WRITE, %rax#执行syscall时,RECORD_SIZE(值为324)用作write(unsigned int fd,const char *buf,size_t count)的第三个参数。movq  $RECORD_SIZE, %rdx                                 syscallret

write-records.s:

#file name: write-record.s
.include "linux.s"
.include "record-def.s" .section .data
record1:.ascii "Fredrick\0".rept 31.byte 0.endr.ascii "Bartlett\0".rept 31.byte 0.endr.ascii "4242 S Prairie\nTulsa, OK 55555\0".rept 209.byte 0.endr.long 45record2:.ascii "Marilyn\0".rept 32.byte 0.endr.ascii "Taylor\0".rept 33.byte 0.endr.ascii "2224 S Johannan St\nChicago, IL 12345\0".rept 203.byte 0.endr.long 29record3:.ascii "Derrick\0".rept 32.byte 0.endr.ascii "McIntire\0".rept 31.byte 0.endr.ascii "500 W Oakland\nSan Diego, CA 54321\0".rept 206.byte 0.endr.long 36file_name:.ascii "test.dat\0".section .text
.globl _start
_start:subq  $8, %rsp                 # Allocate space for the file descriptor on the stackmovq  $SYS_OPEN, %rax          # Open the filemovq  $file_name, %rdi         # Filenamemovq  $0101, %rsi              # Flags: O_WRONLY | O_CREATmovq  $0666, %rdx              # Permissions: 0666syscallmovq  %rax, (%rsp)             # Store the file descriptor on the stack# Write the first recordmovq (%rsp), %rdi              # Load the file descriptormovq $record1, %rsi            # Load the address of the first recordcall  write_record# Write the second recordmovq (%rsp), %rdi              # Load the file descriptormovq $record2, %rsi            # Load the address of the second recordcall  write_record# Write the third recordmovq (%rsp), %rdi              # Load the file descriptormovq $record3, %rsi            # Load the address of the third recordcall  write_record# Close the file descriptormovq  $SYS_CLOSE, %raxmovq  (%rsp), %rdisyscall# Exit the programmovq $SYS_EXIT, %raxmovq  $0, %rdisyscall

二、英语总结

无。

三、其它

今日学习唯一的收获就是使用Chat-GPT解决代码问题。因为书上的代码比较老旧,导致write-records.s编译后运行不起来,一直提示:Segmentation Fault。因为对汇编编程不熟,但又想快速的解决问题,那么Chat-GPT是一个不错的工具,经过Chat-GPT的一番修改,代码已经能运行了,大大节省了分析错误的时间。

四、参考资料

1. 编程

(1)Jonathan Bartlett,《Programming From The Ground Up》:https://book.douban.com/subject/1787855/

2. 英语

(1)Etymology Dictionary:https://www.etymonline.com

(2) Cambridge Dictionary:https://dictionary.cambridge.org

欢迎搜索及关注:编程人(a_codists)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/786671.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

控件与布局

1.控件的分类:主要要6大类一.布局控件:可以容纳多个控件或者嵌套其他布局控件,用于在UI上组织和排列控件。Grid、StackPanel、DockPanel等都属于此类,他们拥有共同的父类Panel二.内容控件:只能容纳一个其他控件或者布局控件作为他的内容。Window、Button等都属于此类,因为…

第7篇:在虚拟机 centos7上搭建jira管理工具

本文详细介绍了如何在CentOS7系统上下载配置Jira,包括创建文件夹、下载安装包、解压、修改配置文件以及设置JVM和MySQL环境。同时,文章还涉及了JDK1.8的安装,数据库的创建,以及Jira的破解步骤,包括替换特定jar文件和配置数据库连接。最后,文章提到了启动Jira服务并进行汉…

最近遇到的一些奇奇怪怪奇技淫巧

然后我CF上绿了————DaisySunchaser要多思考。构造——从哪里入手?CF交互指南要多注意。 我的最初想法:当我查看了他使用了冰茶几的代码: #include<bits/stdc++.h> #define ll long long #define N 200005 #define mp make_pair using namespace std; int T,n,u[N]…

Qt/C++音视频开发81-采集本地麦克风/本地摄像头带麦克风/桌面采集和麦克风/本地设备和桌面推流

一、前言 随着直播的兴起,采集本地摄像头和麦克风进行直播推流,也是一个刚需,最简单的做法是直接用ffmpeg命令行采集并推流,这种方式简单粗暴,但是不能实时预览画面,而且不方便加上一些特殊要求。之前就已经打通了音视频文件和视频流的采集,那是不是可以简单点的方式就能…

如何缩短微信文章链接长度

有时候,我们想把微信公众号的文章发到其他平台上,这时候就需要复制文章的链接。有时候,我们想把微信公众号的文章发到其他平台上,这时候就需要复制文章的链接。 ‍ 手机端复制方式如下: ​ ‍ 微信对于短网址的优化 以前,微信公众号文章的链接特别长。但在 2016 年末,微…

verilog代码与设计总结

Verilog编码风格及设计建议相比于case语句,casez语句将z态看做不关心,casex语句将z态和x态看做不关心。并且所有case类型语句均没有优先级。 锁存器是组合逻辑产生的,一般没有复位端,所以根据其所存特性,在上电的时候没法确定其初始状态,因此正常情况下要避免使用。 组合…

【Azure Logic App】在逻辑应用中开启或关闭一个工作流是否会对其它工作流产生影响呢?

问题描述 使用标准版的Azure Logic App服务,可以创建多个工作流(workflow),如果在启用/禁用其它的工作流时,是否会对正在运行其它工作流造成影响呢? 问题解答 在实际的测验中,我们得到的答案是:会造成影响!在Disabled/Enabled同一个Logic App中的Workflow时,正在运行的…

开源|一款企业应用定制化开发平台,支持企业OA协同办公类信息化系统的建设和开发

前言 在数字化转型的浪潮中,企业面临着多样化的信息系统建设需求。现有的软件系统往往存在定制化程度低、开发周期长、成-本高等问题。此外,随着企业规模的扩大和业务的复杂化,传统的软件系统难以满足灵活多变的业务需 为了解-决这些痛点,企业需要一款能够快速定制、灵活扩…

暑假集训总结 2024

暑假集训总结 2024考试情况:因为身体原因,只参加了29场,表格中标红的是题没改完的 越往后分越低,改题的量也越少,排名和分跟心电图差不多 分低和改题量少不只是因为题难,也有后来状态越来越差,改题的时候很困的原因 为什么排名和分是这样的,主要是心态和答题策略,做不…

Tesla 开发者 API 指南:通过Http发送命令

前言 特斯拉提供两种与汽车通信的方式。一种是使用 API 通过互联网,另一种是使用 BLE 连接。 特斯拉现在只能接受车辆命令 SDK (vehicle command SDK)方式发送命令,该 SDK 使用 Http-Proxy 服务器将命令转发给车辆。除了验证 oAuth 令牌之外,特斯拉正在转向一种更安全的方式…

SFF806A-ASEMI无人机专用SFF806A

SFF806A-ASEMI无人机专用SFF806A编辑:ll SFF806A-ASEMI无人机专用SFF806A 型号:SFF806A 品牌:ASEMI 封装:ITO-220AB 批号:最新 最大平均正向电流(IF):8A 最大循环峰值反向电压(VRRM):600V 最大正向电压(VF):0.95V~0.90V 工作温度:-65C~175C 反向恢复时间:35ns …