Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One obvious mistake (there may be others), is that you you are storing the ASCII value of the digit, yet you are multiplying it as if it were an integer.</p> <p>In other words, the value of the first digit, stored temporarily on the stack is between 48 and 57 (decimal), for respectively "0" through "9".</p> <p>I'm checking for other issues...</p> <p><strong>Edit</strong>: Second pass...<br> I went ahead and provided <strong>an evolutionary version from your snippet</strong>, changing a few things which I found wrong/missing, but also laying the ground for handling more than 2 digits. The main changes are:</p> <ul> <li>Added test for digit 0-9 for 2nd (and subsequent) characters read from input</li> <li>Systematically converted to integer value from ASCII, as soon as digit validity is asserted. (by being consistent we avoid errors)</li> <li>Used the word at top of stack to hold the cumulative value (this way of doing is a bit funky as it causes you to ensure to pop this out, whenever you may branch/exit. More on this late</li> <li>ensured that the high part of the registers were zero-ed out (this may have been causing some of the bugs)</li> <li>We now have only one exit point (for the normal case, i.e. not counting the "PrintError"</li> </ul> <p>Note: this code is untested, indeed not even compiled (don't have MASM here/now...)</p> <pre> ;; ReadNumber ;; Reads a number from the input and returns its value in dx register ;; For now 0-99 values only ;; dh contains 0 = success code ;; dl contains the value ;; (this API should change so that bigger # can be returned) ReadNumber: push ax push bx push cx Lokke: ;; First digit is handled separately, as to to allow for preceding spaces mov ah,01h int 21h cmp al, " " je Lokke ;; skip spaces till first digit cmp al,"0" jb PrintError cmp al,"9" ja PrintError ;; First character is a valid number mov dh, 0 mov dl, al sub dl, "0" push dx ;; *** Cumulative value kept on top of stack *** MoreDigits: mov ah,01h int 21h pop cx ;; in case of premature exit want the stack as expected... cmp al," " je DoneWithDigits cmp al,"0" jb PrintError cmp al,"9" ja PrintError mov dh, 0 ;; good: character is a digit mov dl, al sub dl, "0" ;; down from ASCII mov ax, cx mov bl, 10 mul bl add ax, dx ;; fixed syntax error... push ax ;; *** keep cumulative value on top of stack jmp DoneWithDigits ;; For now only 2 digit max (remove this ;; jmp to try with more) ;; @@@ here to test for overflow / too many char input jmp MoreDigits ;; Almost done DoneWithDigits: pop dx mov dh,0 ;success variable set too true pop ax pop bx pop cx ret </pre> <p>And now, a few more <strong>general considerations</strong></p> <ul> <li>Noted that you do not use any in-memory <strong>variables</strong> (i.e. memory locations that you'd define with "MyLabel dw ?" type syntax, to create "global" variables), or even local variables (found at relative offset of the frame pointer). This is maybe just to ease into assembly programming etc. and/or a requirement of your (?) assignment. No rush to get into addressing such variables, but you'll find these handy when you do.</li> <li><strong>Calling convention</strong>: The ReadNumber() methods seems to be in charge of safeguarding the ax, bx and cx registers. (and to reestablish these before exiting). This is a bit of an odd convention, typically the caller to a method pushes his own context onto the stack (and then pushes any parameter to the function, if any). Anyway you'll see these conventions in action, along with the way the frame pointer register (BP) is used etc. Maybe check this <a href="http://en.wikipedia.org/wiki/Stack_frame#Structure" rel="nofollow noreferrer">wikipedia article</a> as a preview (BTW, depending on the assembler you use in particular if this is a macro assembler, you can get much of this mechanics taken care of.) No rush to do all this, first get familiar with basics assembly.</li> <li>the <strong>layout of the code</strong> is a bit strange, with assembly we typically like the 4 column layout with the labels in the first column, nice and visible, then the Instructions, followed by the operands, and last the line comments.</li> </ul> <p>Hope this helps,<br> now, have fun!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload