首页 星云 工具 资源 星选 资讯 热门工具
:

PDF转图片 完全免费 小红书视频下载 无水印 抖音视频下载 无水印 数字星空

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

编程知识
2024年08月24日 17:35

《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 %rbp
    mov %rsp, %rbp

    push %rbx
    mov ST_FILEDES(%rbp), %rdi
    mov ST_READ_BUFFER(%rbp), %rsi
    mov $RECORD_SIZE, %rdx
    mov $SYS_READ, %rax
    syscall
    pop %rbx
    
    mov %rbp, %rsp
    pop %rbp
    ret

read-records.s:

#file name: read-records.s

.include "linux.s"
.include "record-def.s"

.section .data

filename:
    .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, -16

    mov %rsp, %rbp

    # open ch6/test.dat
    mov $SYS_OPEN, %rax
    mov $filename, %rdi
    mov $0, %rsi
    mov $0666, %rdx
    syscall

    push %rax       # push input file descriptor onto stack
    push $STDOUT    # push output file descriptor onto stack

record_read_loop:
    # invoke read_record function
    push INPUT_DESCRIPTOR(%rbp)
    push $RECORD_BUFFER
    call read_record
    add $16, %rsp       # pop function args off of stack

    cmp $RECORD_SIZE, %rax
    jne finished_reading

    push $RECORD_FIRSTNAME + RECORD_BUFFER
    call count_chars
    add $8, %rsp        

    mov %rax, %rdx                      # count of chars to print
    mov $RECORD_BUFFER, %rsi
    mov OUTPUT_DESCRIPTOR(%rbp), %rdi
    mov $SYS_WRITE, %rax
    syscall

    mov $1, %rdx                      # count of chars to print
    mov $newline, %rsi
    mov OUTPUT_DESCRIPTOR(%rbp), %rdi
    mov $SYS_WRITE, %rax
    syscall

    jmp record_read_loop

finished_reading:
    mov $SYS_EXIT, %rax
    mov $0, %rdi
    syscall

(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, @function
	
write_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                                 
    syscall

    ret

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 45
    
record2:
    .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 29
    
record3:
    .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 36

file_name:
    .ascii "test.dat\0"
    
.section .text
.globl _start
_start:
    subq  $8, %rsp                 # Allocate space for the file descriptor on the stack
    
    movq  $SYS_OPEN, %rax          # Open the file
    movq  $file_name, %rdi         # Filename
    movq  $0101, %rsi              # Flags: O_WRONLY | O_CREAT
    movq  $0666, %rdx              # Permissions: 0666
    syscall

    movq  %rax, (%rsp)             # Store the file descriptor on the stack

    # Write the first record
    movq (%rsp), %rdi              # Load the file descriptor
    movq $record1, %rsi            # Load the address of the first record
    call  write_record

    # Write the second record
    movq (%rsp), %rdi              # Load the file descriptor
    movq $record2, %rsi            # Load the address of the second record
    call  write_record

    # Write the third record
    movq (%rsp), %rdi              # Load the file descriptor
    movq $record3, %rsi            # Load the address of the third record
    call  write_record

    # Close the file descriptor
    movq  $SYS_CLOSE, %rax
    movq  (%rsp), %rdi
    syscall

    # Exit the program
    movq $SYS_EXIT, %rax
    movq  $0, %rdi
    syscall

二、英语总结

无。

三、其它

今日学习唯一的收获就是使用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)

From:https://www.cnblogs.com/codists/p/18378059
本文地址: http://www.shuzixingkong.net/article/1402
0评论
提交 加载更多评论
其他文章 AD(Active Directory )域的搭建与操作
AD 域的搭建与操作 一、准备工作 准备好 VM 虚拟机和 Server 的安装包。 二、安装 Server 2022 选择标准且有图形界面的进行安装。 选择自定义安装方式。 为虚拟机 server2022 安装 VMware tools。 回到桌面,右键个性化把计算机和网络图标放出来。 三、安装
【Azure Logic App】在逻辑应用中开启或关闭一个工作流是否会对其它工作流产生影响呢?
问题描述 使用标准版的Azure Logic App服务,可以创建多个工作流(workflow),如果在启用/禁用其它的工作流时,是否会对正在运行其它工作流造成影响呢? 问题解答 在实际的测验中,我们得到的答案是:会造成影响!在Disabled/Enabled同一个Logic App中的Workfl
【Azure Logic App】在逻辑应用中开启或关闭一个工作流是否会对其它工作流产生影响呢? 【Azure Logic App】在逻辑应用中开启或关闭一个工作流是否会对其它工作流产生影响呢? 【Azure Logic App】在逻辑应用中开启或关闭一个工作流是否会对其它工作流产生影响呢?
PG数据库导致断电/重启无法正常启动问题排查
PG数据库导致断电/重启无法正常启动问题排查 一、问题 数据库断电后,启动PG数据库后无法正常启动,报”psql: could not connect to server: No such file or directory”的错误,错误图片如下: 二、背景分析 数据库是单机版,使用k8s进行部署运
PG数据库导致断电/重启无法正常启动问题排查 PG数据库导致断电/重启无法正常启动问题排查 PG数据库导致断电/重启无法正常启动问题排查
Go 互斥锁 Mutex 源码分析(二)
原创文章,欢迎转载,转载请注明出处,谢谢。 0. 前言 在 Go 互斥锁 Mutex 源码分析(一) 一文中分析了互斥锁的结构和基本的抢占互斥锁的场景。在学习锁的过程中,看的不少文章是基于锁的状态解释的,个人经验来看,从锁的状态出发容易陷入细节,了解锁的状态转换过一段时间就忘,难以做到真正的理解。想
Go 互斥锁 Mutex 源码分析(二) Go 互斥锁 Mutex 源码分析(二) Go 互斥锁 Mutex 源码分析(二)
线性dp:LeetCode674. 最长连续递增序列
LeetCode674. 最长连续递增序列 阅读本文之前,需要先了解“动态规划方法论”,这在我的文章以前有讲过 链接:动态规划方法论 本文之前也讲过一篇文章:最长递增子序列,这道题,阅读本文的同时可以与“最长递增子序列进行对比”,这样更能对比二者的区别! LeetCode300.最长递增子序列 -
Python 潮流周刊#66:Python 的预处理器(摘要)
本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章、教程、开源项目、软件工具、播客和视频、热门话题等内容。愿景:帮助所有读者精进 Python 技术,并增长职业和副业的收入。 分享了 12 篇文章,12 个开源项目,1 则音视频,全文 2100 字。 以下是
DDD是软件工程的第一性原理?
本文书接上回《DDD建模后写代码的正确姿势》,关注公众号(老肖想当外语大佬)获取信息: 最新文章更新; DDD框架源码(.NET、Java双平台); 加群畅聊,建模分析、技术实现交流; 视频和直播在B站。 前提 本文需要以系列前文的逻辑链条和结论为前提,如果没有阅读过前文的,可以阅读合集《老肖的领域
DDD是软件工程的第一性原理? DDD是软件工程的第一性原理? DDD是软件工程的第一性原理?
k8s新版本使用container而不是docker导致创建pod一直提示证书问题
使用 Harbor 仓库作为 Kubernetes 集群私有仓库 Harbor 仓库信息 内网地址:hub.rainsc.com IP 地址:192.168.66.100 问题背景 在许多版本的教程中,会建议在 Docker 的配置中添加忽略证书的列表。然而,截至 2024 年 8 月 24 日,这