1

i am new to MASM assembly language programming. i have a delay function of a hardware system in MASM but i am not able to understand how it is working. can you explain line by line by adding comments how it works code is:

delay proc 
mov bx,02fffh 
l2: mov di,0ffffh 
l1: dec di 
jnz l1 
dec bx 
jnz l2 
ret
delay endp

there are very few sites where they have MASM related stuff so any help will be very useful.

rishuverma
  • 79
  • 1
  • 7
  • 4
    2 nested loops doing - pretty much nothing, just looping – Tommylee2k Jun 08 '18 at 10:17
  • every instruction takes some time to execute, so if you execute many instructions, you get "delay". This kind of "do nothing" delay is not very productive, because it means the CPU can't execute some "serious" calculation meanwhile, also on modern x86 it is very difficult to estimate exact amount of time which will be spend to execute group of instructions, but sometimes a loop like this can help, although usually there are better (performance and accuracy) options (which require more complex code and/or OS support). – Ped7g Jun 08 '18 at 10:50
  • 1
    "very few sites where they have MASM related stuff" you search for the wrong things. MASM is just Microsoft's assembler program, some kind of 'compiler'; Try searching for "8086 assembler" or "x86 assembler" instead – Tommylee2k Jun 08 '18 at 10:56
  • @Ped7g: *every instruction takes some time to execute*. Not always true. If there's extra front-end bandwidth in a loop that bottlenecks on the latency of a loop-carried dependency chain, independent instructions won't slow it down. e.g. adding a `nop` or three into the inner loop wouldn't make it slower on modern x86, unlike on a 486 or P5 Pentium. This should run pretty reliably at 1 iteration per clock, unless it ends up split across a 32-byte boundary on a Skylake/Kaby Lake (loop buffer disabled by a microcode update). – Peter Cordes Jun 08 '18 at 11:19
  • But delay loops are mostly bogus because the clock speed can change on the fly. For delays of hundreds of nanoseconds, spinning on RDTSC will give pretty reliable wall-clock delays. [How much delay is generated by this assembly code in linux](https://stackoverflow.com/q/49924102) – Peter Cordes Jun 08 '18 at 11:22
  • @PeterCordes that `nop` did still take some time to execute, it's just that time was shared/parallel with other instructions or amortized already in the extra front-end, but some time was spent. It's not like it's physically "for free". – Ped7g Jun 08 '18 at 11:42
  • 1
    @Ped7g: that's fair, taking time in parallel with other instructions is a good way to look at it. That's the whole point of a superscalar CPU. Amortized isn't the right description, though. – Peter Cordes Jun 08 '18 at 12:13

1 Answers1

0
delay proc         ;start of function
mov bx,02fffh      ;Load literal into register bx
l2: mov di,0ffffh  ;Load literal into register di
l1: dec di         ;decrement di
jnz l1             ;Jump to l1 if di is not zero
dec bx             ;decrement bx
jnz l2             ;Jump to l2 if bx is not zero
ret                ;Return to caller
delay endp         ;end of function

It is an nested loop.

Gerhard
  • 6,850
  • 8
  • 51
  • 81
  • can you pls explain how much delay does 02fffh and 0ffffh signifies. and how do i adjust that like if i want delay of 1 sec or 5sec how do i select those values? – rishuverma Jun 08 '18 at 12:09
  • Translating that to specific times would be require exact knowledge of your hardware setup. You have provide no such information. – Gerhard Jun 11 '18 at 09:38