(Assuming that you want to keep the dependency on the ebp register, which might be handy if you want to make the upper limit variable.
#EMU8086 EXERCISES CODE#
In this case, you won't see any performance improvement, but these instructions are still something to be aware of and this does make the code slightly easier to read. In some cases, conditional moves will make the code faster by eliminating the possibility of branch mispredictions.
#EMU8086 EXERCISES PRO#
If you are targeting the Pentium Pro or later (so basically, any modern x86 processor) you can use conditional move instructions like so: cmp eax, ebp Jle createSquareNumbers if not, skip the next line You can simply do: cmp eax, 1000000 see if input is over the upper limit I further assume that ebp is actually just 1000000, in which case this portion of your code is overly verbose (and thus inefficient). In fact, in the current implementation, the contents of the ebp register are a pre-condition for the function, so its use should unarguably be documented as part of the function's signature. For example, in this code, ebp apparently contains the "upper limit", but other than assuming that the code works and reverse-engineering its logic, I have no basis for that assumption. In other cases, it is not quite so obvious. Certain assumptions can be made based on common calling conventions (like that functions will return their result in eax), but that should still be documented for clarity. The biggest problem here-and generally with assembly-is the use of registers in the code without explicit documentation of what those registers contain, e.g., in the form of comments. start:Ĭmp eax, ebp When the number received is smaller the upper limit. I suggest you make all the instructions, operands, and comments start in their own columns. You should write in a more readable fashion. You should use this overflow ( EDX 0) as an extra reason to exit the loop. You should test for overflow on the multiplication! You are printing results from the EAX register only, but look what happens when the square exceeds the 32 bits of EAX. Prior to the multiplication it is not necessary to zero the edx register. (Why else would you move it to ecx when the input is too large?) Then it would be better to not use the immediate value and just write: cmp eax, ebp I suspect that the upper limit is in the ebp register. So therefore: Hints and comments from more experienced Assembly programmers very appreciated. Prints eax to stdin.īut I'm sure there's a lot of room for improvements. Mul ebx Multiply the number with itself.Ĭall print_eax Provided by teacher. otherwise you override it with the upper limit. Reads in a hexadecimal number from stdin.Ĭmp eax, 1000000 When the number received is smaller the upper limit. Example -Ĭall read_hex Provided by teacher. = Displays the square number from 1 until = Trying to explain my thoughts and motivations. The program then calculates and prints the square numbers from 1 to the given number." "Write a program which takes an integer given by the user. Inspired by an exercise I had to do once in school when learning PHP. Do check this!Īlthough this works here (.I try to learn (flat-, FASM-) Assembly currently.įor to become more familiar with the concept of branching I made a tiny program. If a file can't be opened, DOS returns the CF set. With these data definitions: COM_FILE db 'C:\EMU8086\MyBuild\*.com', 0 The solution is to append the filename that DOS gave you to the same path that was used to ask for the file: mov si, FNAMEĬmp al, 0 Copy includes the terminating zero If you still can't open the file it's probably because on the one hand you searched for it in a s p e c i f i c directory ( C:\emu8086\MyBuild) but on the other hand you try to open it in the c u r r e n t directory ( ?). Assembly files is in C:\emu8086\MySource. I have installed emu8086 on C:\emu8086 and all the COM files I want to open is on C:\emu8086\MyBuild. You can always retrieve the current DTA address via function 2Fh. If however the DTA address was changed (normally that's something you would have done yourself via function 1Ah) you need to use the other address. If DOS found any matching file, its ASCIIZ filename will be at address 9Eh. Since the default DTA (Disk Transfer Area) is at 80h, you're right to say: FNAME equ 9EH. Use this ASCIIZ filespec with wildcard: COM_FILE db 'C:\EMU8086\MyBuild\*.com', 0 In order to search for any normal files specify CX=0: mov ah, 4Eh The DOS.SearchFirst function needs the CX parameter.