欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 《Programming from the Ground Up》阅读笔记:p103-p116

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

2025/5/1 22:25:38 来源:https://blog.csdn.net/codists/article/details/141503149  浏览:    关键词:《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)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词