{"id":44223,"date":"2015-04-16T07:00:00","date_gmt":"2015-04-16T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2015\/04\/16\/why-did-the-original-code-for-find-com-use-lop-as-a-label-instead-of-loop\/"},"modified":"2019-03-13T12:14:45","modified_gmt":"2019-03-13T19:14:45","slug":"20150416-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150416-00\/?p=44223","title":{"rendered":"Why did the original code for FIND.COM use lop as a label instead of loop?"},"content":{"rendered":"<p>A few years ago, I left you with an exercise: Given the code <\/p>\n<pre>\n        mov     dx,st_length            ;length of the string arg.\n        dec     dx                      ;adjust for later use\n        mov     di, line_buffer\nlop:\n        inc     dx\n        mov     si,offset st_buffer     ;pointer to beg. of string argument\n\ncomp_next_char:\n        lodsb\n        cmp     al,byte ptr [di]\n        jnz     no_match\n\n        dec     dx\n        jz      a_matchk                ; no chars left: a match!\n        call    next_char               ; updates di\n        jc      no_match                ; end of line reached\n        jmp     comp_next_char          ; loop if chars left in arg.\n<\/pre>\n<p><a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/08\/25\/10200026.aspx\">why is the loop label called <code>lop<\/code> instead of <code>loop<\/code><\/a>? <\/p>\n<p>The answer is that calling it <code>loop<\/code> would create ambiguity with the 8086 instruction <code>loop<\/code>. <\/p>\n<p>Now, you might say (<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/08\/25\/10200026.aspx#10200673\">if your name is Worf<\/a>), that there is no ambiguity. &#8220;Every line consists of up to four things (all optional). A label, an instruction\/pseudo-instruction, operands, and comments. The label is optionally followed by a colon. If there is no label, then the line must start with whitespace.&#8221; <\/p>\n<p>If those were the rules, then there would indeed be no ambiguity. <\/p>\n<p>But those aren&#8217;t the rules. Leading whitespace is not mandatory. If you are so inclined, you can choose to begin your instructions all in column zero. <\/p>\n<pre>\nmov dx,st_length\ndec dx\nmov di, line_buffer\nlop:\ninc dx\nmov si,offset st_buffer\ncomp_next_char:\nlodsb\ncmp al,byte ptr [di]\njnz no_match\ndec dx\njz a_matchk\ncall next_char\njc no_match\njmp comp_next_char\n<\/pre>\n<p>It&#8217;s not recommended, but it&#8217;s legal. (I have been known to do this when hard-coding breakpoints for debugging purposes. That way, a search for <code>\/^int 3\/<\/code> will find all of my breakpoints.) <\/p>\n<p>Since you can put the opcode in column zero, a line like this would be ambiguous: <\/p>\n<pre>\nloop ret\n<\/pre>\n<p>This could be parsed as &#8220;Label this line <code>loop<\/code> and execute a <code>ret<\/code> instruction.&#8221; Or it could be parsed as &#8220;This is an unlabeled line, consisting of a <code>loop<\/code> instruction that jumps to the label <code>ret<\/code>.&#8221; <\/p>\n<\/p>\n<table CELLPADDING=\"3\" STYLE=\"border-collapse: collapse\">\n<tr>\n<th STYLE=\"border: solid 1px black;border-top-color: transparent\">Label<\/th>\n<th STYLE=\"border: solid 1px black;border-top-color: transparent\">Opcode<\/th>\n<th STYLE=\"border: solid 1px black;border-top-color: transparent\">Operand<\/th>\n<\/tr>\n<tr>\n<td><code>loop<\/code><\/td>\n<td><code>ret<\/code><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td COLSPAN=\"3\" ALIGN=\"center\">&ndash; or &ndash;<\/td>\n<tr>\n<td><\/td>\n<td><code>loop<\/code><\/td>\n<td><code>ret<\/code><\/td>\n<\/tr>\n<\/table>\n<p>Disallowing instruction names as labels or macros or equates is the simplest way out of this predicament. Besides, you probably shouldn&#8217;t be doing it anyway. <a HREF=\"http:\/\/www.cs.earlham.edu\/~skylar\/humor\/Unix\/c.language.pranks.html\">Imagine the havoc<\/a> if you did <\/p>\n<pre>\nor equ and\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An old puzzle.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-44223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>An old puzzle.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/44223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=44223"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/44223\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=44223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=44223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=44223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}