Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercise: reimplement writing a string to a stream #22

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions subx/057write.subx
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,48 @@ $write:fake:
50/push-EAX
51/push-ECX
52/push-EDX
53/push-EBX
# ECX = f
8b/copy 1/mod/*+disp8 5/rm32/EBP . . 1/r32/ECX 8/disp8 . # copy *(EBP+8) to ECX

# EAX = s
8b/copy 1/mod/*+disp8 5/rm32/EBP . . 0/r32/EAX 0xc/disp8 . # copy *(EBP+12) to EAX

# push s (arg 3)
50/push-EAX

# EDX = f
8b/copy 1/mod/*+disp8 5/rm32/EBP . . 2/r32/EDX 8/disp8 . # copy *(EBP+8) to EDX

# ECX = f->length
8b/copy 1/mod/*+disp8 2/rm32/EDX . . 1/r32/ECX 8/disp8 . # copy *(EDX+8) to ECX

# ECX = &f->data[f->length]
8d/copy-address 1/mod/*+disp8 4/rm32/sib 2/base/EDX 1/index/ECX . 1/r32/ECX 0xc/disp8 . # copy EDX+ECX+12 to ECX

# push &f->data[f->length] (arg 2)
51/push-ECX

# EDX = f->write
8b/copy 0/mod/indirect 1/rm32/ECX . . . 2/r32/EDX . . # copy *ECX to EDX
# EBX = f->length
8b/copy 1/mod/*+disp8 1/rm32/ECX . . . 3/r32/EBX 8/disp8 . # copy *(ECX+8) to EBX
8b/copy 0/mod/indirect 2/rm32/EDX . . 1/r32/EDX . . # copy *(EDX) to ECX

# ECX = &f->data[f->write]
8d/copy-address 1/mod/*+disp8 4/rm32/sib 2/base/EDX 1/index/ECX . 1/r32/ECX 0xc/disp8 . # copy EDX+ECX+12 to ECX

# push &f->data[f->write] (arg 1)
51/push-ECX

# EAX = _append-3(&f->data[f->write], &f->data[f->length], s)
# . . push s
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 0xc/disp8 . # push *(EBP+12)
# . . push &f->data[f->length]
8d/copy-address 1/mod/*+disp8 4/rm32/sib 1/base/ECX 3/index/EBX . 3/r32/EBX 0xc/disp8 . # copy ECX+EBX+12 to EBX
53/push-EBX
# . . push &f->data[f->write]
8d/copy-address 1/mod/*+disp8 4/rm32/sib 1/base/ECX 2/index/EDX . 3/r32/EBX 0xc/disp8 . # copy ECX+EDX+12 to EBX
53/push-EBX
# . . call
e8/call _append-3/disp32
e8/call _append-3/disp32

# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP

# f->write += EAX
01/add 0/mod/indirect 1/rm32/ECX . . . 0/r32/EAX . . # add EAX to *ECX
01/add 0/mod/indirect 2/rm32/EDX . . . 0/r32/EAX . . # add EAX to *EDX

# . restore registers
5b/pop-to-EBX
5a/pop-to-EDX
59/pop-to-ECX
58/pop-to-EAX

$write:end:
# . epilog
89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . . # copy EBP to ESP
Expand Down