Question

I'm currently presented with implementing a program that takes an input of a grade value (ex. 75) and then outputs a letter grade corresponding to it. I've implemented the following requested scale via an array / table:

.data
 table BYTE 89d, 'A'
  BYTE 79d,  'B'
  BYTE 69d,  'C'
  BYTE 59d,  'D'
  BYTE 0d,  'F'
NumCols = 2
NumRows = 5
user_ip BYTE ?
message1 BYTE "Enter a grade value: ", 0h
message2 BYTE "The Grade of ", 0h
message3 BYTE " Yields A Letter Grade of ", 0h

I'm using the following code to sort through this array / table and output the letter grade.

mov edx, OFFSET message1
call WriteString
call readDec
mov user_ip, al

mov esi, OFFSET user_ip
mov edi, OFFSET table
mov ecx, NumRows

L1:
    CMPSB
    jae L2
    add edi, NumCols
Loop L1

L2:
    mov edx, OFFSET message2
    call WriteString
    mov al, user_ip
    call WriteDec
    mov edx, OFFSET message3
    call WriteString
    mov edx, edi
    call WriteString
    call Crlf

With an input of 75, I'm being presented with: "The Grade of 75 Yields A Letter Grade of EC;D". The Program also temporarily stops working.

I'm confident it has something to do with pointers and data sizes. My ideal goal is to store the value of the letter grade in a variable, but I can't seem to find a way to do it given the data size needed to use pointers. Any ideas of how to do this?

Était-ce utile?

La solution

You are calling WriteString but the values in your table are characters not strings. The difference in this case is that they are not zero terminated. Either use WriteChar if you have that, or put a zero in your table but then don't forget to adjust NumCols too.

Also note that CMPSB increments both pointers which means your comparisons will be wrong. You should probably just use the non-string CMP especially since the user_ip is already in register AL.

PS: Finally somebody who uses a table :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top