The following 8088/8086 assembly program tests the modular-sum checksum of a block of data. I suspect it could be tighter; can anyone do it in fewer bytes?
31 xor ax,ax ; zero the sum
c0
bb mov bx,2000h ; count 8kb of ROM
00
20
4b dec bx ; start at the top
02 add al,[bx] ; add the current byte to the total
07
85 test bx,bx ; see if we're at the bottom yet
db
75 jnz -7 ; loop while not done
f9
84 test al,al ; sum should be 0
c0
74 jz 1 ; if sum is good, continue with program
01
f4 hlt ; if sum is bad, stop
2000h
instead of just2000
? Also, you could rework the code to use the flags that get set bydec
to figure out if you should keep looping rather than doingtest
. \$\endgroup\$cld
? Is there unknown code that executes before this that has a habit of changing and not restoring this? I'd certainly expect the power-on default for any 8086 chip to be cld. \$\endgroup\$cld
versus omitting it means that you trade off a single byte for the potential future pain of having things run just fine most of the time until some other function is called that changes the direction flag before this code is invoked. Good luck untangling that runtime nightmare. I'd rather pay the price for the byte and the additional clarity, but that's just me. \$\endgroup\$