record_def.s
1 .equ RECORD_FIRSTNAME, 0 #记录数据:firstName:40字节 0~39 2 .equ RECORD_LASTNAME, 40 #记录数据:lastName:40字节 40~79 3 .equ RECORD_ADDRESS, 80 #记录数据:address:240字节 80~319 4 .equ RECORD_AGE, 320 #记录数据:age:4字节 320~323 5 .equ RECORD_SIZE, 324 #记录数据:size:324字节 0~323
linuxg.s
1 #文件名称:linuxg。s 2 3 #系统调用中断号: 4 .equ SYS_EXIT, 1 5 .equ SYS_READ, 3 6 .equ SYS_WRITE, 4 7 .equ SYS_OPEN, 5 8 .equ SYS_CLOSE, 6 9 .equ SYS_BRK, 45 10 11 #调用Linux中断号: 12 .equ LINUX_SYSCALL, 0x80 13 14 #标准文件描述符号 15 .equ STDIN, 0 16 .equ STDOUT, 1 17 .equ STDERR, 2 18 19 .equ END_OF_FILE, 0
read_record.s
1 .include "record_def.s" 2 .include "linuxg.s" 3 4 .equ ST_READ_BUFFER, 8 5 .equ ST_FILEDES, 12 6 .section .text 7 .globl read_record 8 .type read_record, @function 9 read_record: 10 pushl %ebp 11 movl %esp, %ebp 12 13 pushl %ebx 14 movl ST_FILEDES(%ebp), %ebx 15 movl ST_READ_BUFFER(%ebp), %ecx 16 movl $RECORD_SIZE, %edx 17 movl $SYS_READ, %eax 18 int $LINUX_SYSCALL 19 20 popl %ebx 21 22 movl %ebp, %esp 23 popl %ebp 24 ret
write_record.s
1 .include "record_def.s" 2 .include "linuxg.s" 3 4 .equ ST_WRITE_BUFFER, 8 5 .equ ST_FILEDES, 12 6 .section .text 7 .globl write_record 8 .type write_record, @function 9 write_record: 10 pushl %ebp 11 movl %esp, %ebp 12 13 pushl %ebx 14 movl $SYS_WRITE, %eax 15 movl ST_FILEDES(%ebp), %ebx 16 movl ST_WRITE_BUFFER(%ebp), %ecx 17 movl $RECORD_SIZE, %edx 18 int $LINUX_SYSCALL 19 20 popl %ebx 21 22 movl %ebp, %esp 23 popl %ebp 24 ret
write_records.s
1 .include "linuxg.s" 2 .include "record_def.s" 3 4 .section .data 5 6 record1: 7 .ascii "Fredrick\0" 8 .rept 31 #填充到40字节 9 .byte 0 10 .endr 11 12 .ascii "Bartlett\0" 13 .rept 31 14 .byte 0 15 .endr 16 17 .ascii "4242 S Prairie\nTulsa, OK 55555\0" 18 .rept 209 #填充到240字节 19 .byte 0 20 .endr 21 22 .long 45 23 24 record2: 25 .ascii "Marilyn\0" 26 .rept 32 #填充到40字节 27 .byte 0 28 .endr 29 30 .ascii "Taylor\0" 31 .rept 33 32 .byte 0 33 .endr 34 35 .ascii "2224 S Johannan St\nChicago, IL 12345\0" 36 .rept 203 #填充到240字节 37 .byte 0 38 .endr 39 40 .long 29 41 42 record3: 43 .ascii "Derrick\0" 44 .rept 32 #填充到40字节 45 .byte 0 46 .endr 47 48 .ascii "McIntire\0" 49 .rept 31 50 .byte 0 51 .endr 52 53 .ascii "500 W Oakland\nSan Diego, CA 54321\0" 54 .rept 206 #填充到240字节 55 .byte 0 56 .endr 57 58 .long 36 59 60 fileName: #书上代码错误,另外添加 61 .ascii "test.dat\0" 62 .equ ST_FILE_DESCRIPTOR, -4 63 .globl _start 64 _start: 65 movl %esp, %ebp 66 subl $4, %esp 67 68 #打开文件 69 movl $SYS_OPEN, %eax 70 movl $fileName, %ebx 71 movl $0101, %ecx 72 movl $0666, %edx 73 int $LINUX_SYSCALL 74 75 movl %eax, ST_FILE_DESCRIPTOR(%ebp) 76 77 #写入第一条记录 78 pushl ST_FILE_DESCRIPTOR(%ebp) 79 pushl $record1 80 call write_record 81 addl $8, %esp 82 83 #写入第二条记录 84 pushl ST_FILE_DESCRIPTOR(%ebp) 85 pushl $record2 86 call write_record 87 addl $8, %esp 88 89 #写入第三条记录 90 pushl ST_FILE_DESCRIPTOR(%ebp) 91 pushl $record3 92 call write_record 93 addl $8, %esp 94 95 #关闭文件 96 movl $SYS_CLOSE, %eax 97 movl ST_FILE_DESCRIPTOR(%ebp), %ebx 98 int $LINUX_SYSCALL 99 100 #退出文件 101 movl $SYS_EXIT, %eax 102 movl $0, %ebx 103 int $LINUX_SYSCALL
原文地址:https://www.cnblogs.com/guochaoxxl/p/11224739.html
时间: 2024-10-30 15:11:54