当前位置: 首页 > news >正文

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

《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)

相关文章:

  • Linux内核定时器
  • Java--Zuul网关中的过滤器
  • AIGC深度学习教程:Transformer模型中的Position Embedding实现与应用
  • IO与进程
  • 通信系统收发原理冷知识
  • Datawhale X 李宏毅苹果书 AI夏令营(深度学习入门)taks2
  • 跟《经济学人》学英文:2024年08月24日这期 What to make of America’s topsy-turvy economy
  • centos7安装Kafka单节点环境部署三-安装Logstash
  • MURF860AC-ASEMI智能AI专用MURF860AC
  • 虚幻游戏开发| 编辑器内正常运行但打包出错
  • 高级java每日一道面试题-2024年8月23日-框架篇[SpringBoot篇]-什么是JavaConfig?
  • ACM模式下算法题输入输出攻略【C++】
  • Adobe Lightroom Classic (LRC) 软件下载安装和软件使用介绍
  • 【Java】/* 与树有关的一些概念 */
  • u盘突然说要格式化才能访问?如何跳过格式化打开U盘
  • create-react-app做的留言板
  • CSS魔法堂:Absolute Positioning就这个样
  • django开发-定时任务的使用
  • echarts花样作死的坑
  • Elasticsearch 参考指南(升级前重新索引)
  • hadoop集群管理系统搭建规划说明
  • HashMap ConcurrentHashMap
  • HTTP中GET与POST的区别 99%的错误认识
  • JavaScript新鲜事·第5期
  • JAVA并发编程--1.基础概念
  • MySQL主从复制读写分离及奇怪的问题
  • Rancher-k8s加速安装文档
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 大型网站性能监测、分析与优化常见问题QA
  • 海量大数据大屏分析展示一步到位:DataWorks数据服务+MaxCompute Lightning对接DataV最佳实践...
  • 回顾 Swift 多平台移植进度 #2
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • PostgreSQL 快速给指定表每个字段创建索引 - 1
  • 阿里云服务器购买完整流程
  • (175)FPGA门控时钟技术
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (二) Windows 下 Sublime Text 3 安装离线插件 Anaconda
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (七)c52学习之旅-中断
  • (十八)三元表达式和列表解析
  • .NET Core 中的路径问题
  • .NET Framework 3.5安装教程
  • .net 获取某一天 在当月是 第几周 函数
  • .NET/C# 使用反射注册事件
  • .net打印*三角形
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
  • .NET序列化 serializable,反序列化
  • @KafkaListener注解详解(一)| 常用参数详解
  • [AIR] NativeExtension在IOS下的开发实例 --- IOS项目的创建 (一)
  • [boost]使用boost::function和boost::bind产生的down机一例
  • [BUUCTF 2018]Online Tool(特详解)
  • [C++]C++基础知识概述
  • [CTSC2014]企鹅QQ
  • [Docker]十二.Docker consul集群搭建、微服务部署,Consul集群+Swarm集群部署微服务实战