TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Write assembly that opens each file in a dir and changes foo with bar

3 pointsby frogger8over 2 years ago
Write an assembly program that opens each file in a directory and then changes all of the occurences of foo with bar.<p>;Open a file handle<p>mov ax, 3Dh<p>mov dx, offset filename<p>int 21h<p>;Check that the file handle is valid<p>cmp ax, 0<p>jne file_open<p>;Start looping through the directory<p>mov si, offset directory<p>mov di, offset filename<p>loop:<p>mov al, [si]<p>cmp al, 0<p>je exit<p>;Copy the filename<p>mov [di], al<p>inc si<p>inc di<p>jmp loop<p>file_open:<p>;Read the file and search for &quot;foo&quot;<p>mov bx, ax<p>mov ah, 3Fh<p>mov cx, 0FFFFh<p>mov dx, offset buffer<p>int 21h<p>;Check if the end of file has been reached<p>cmp ax, 0<p>je exit<p>;Loop through the read buffer<p>mov si, offset buffer<p>mov di, offset replace<p>loop2: mov al, [si] cmp al, &#x27;f&#x27; je check_foo inc si jmp loop2<p>check_foo: cmp byte [si + 1], &#x27;o&#x27; jne loop2 cmp byte [si + 2], &#x27;o&#x27; jne loop2<p>;Replace &quot;foo&quot; with &quot;bar&quot; mov [di], &#x27;b&#x27; inc di mov [di], &#x27;a&#x27; inc di mov [di], &#x27;r&#x27; inc di jmp loop2<p>;Write the modified buffer to the file mov bx, ax mov ah, 40h mov cx, 0FFFFh mov dx, offset replace int 21h<p>;Go to the next file jmp file_open<p>exit: ;Close the file mov ah, 3Eh int 21h<p>;Exit mov ax, 4C00h int 21h

1 comment

frogger8over 2 years ago
OP here, I asked… even chatGPT doesn’t know how to make the new lines format properly on HN :)