当前位置: 首页 > 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)


http://www.mrgr.cn/news/10778.html

相关文章:

  • 使用在AMD GPU上运行的ROCm进行大语言模型的自然语言处理任务
  • 数据分析:numpy 数组的基本使用
  • YOLOv8多种方法改进CBAM注意力机制有效提升检测精度(已跑通)
  • [Android studio]无法联网
  • Linux常用应急排查命令(持续更新)
  • ElasticSearch 的单点部署环境搭建
  • CSS学习笔记(01)flex布局
  • MFC之word操作
  • [数据集][目标检测]电力场景输电线均压环歪斜检测数据集VOC+YOLO格式303张2类别
  • 打手机检测算法源码样本展示打手机检测算法实际应用场景介绍
  • 人脸质量评价:深入解析和实现
  • 【Node】m1 mac 使用 nvm 安装 node v14 报错
  • 容器的ip地址不稳定问题、联合文件系统、核对时间、制作基础镜像
  • 突破编程:深入理解C++中的组合模式
  • docker容器run命令设置
  • Linux中断管理
  • STC89C52 定时器浅谈
  • 树数据结构(Tree Data Structures)的全面指南:深度解析、算法实战与应用案例
  • 深度优先搜索模板
  • 【Material-UI】Radio Group中的 Label Placement 属性详解