Guest User

Untitled

a guest
Apr 15th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MPASM 7.64 KB | None | 0 0
  1. TITLE lab3              (lab3.asm)
  2. ; William Keller (#996964933) wjkeller@ucdavis.edu
  3.  
  4. INCLUDE Irvine32.inc
  5. .data
  6.     lab_grades  BYTE 6 DUP(?)
  7.     hw_grades   BYTE 6 DUP(?)
  8.     exam_grades     BYTE 3 DUP(?)
  9.  
  10.     lab_intro   BYTE "Enter the six lab grades: ",0
  11.     lab_prefix_s    BYTE "Lab",0
  12.     lab_post_s  BYTE ": ",0
  13.    
  14.     hw_intro    BYTE "Enter the six Hw grades: ",0
  15.     hw_prefix_s     BYTE "Hw",0
  16.     hw_post_s   BYTE ": ",0
  17.  
  18.     exam_intro  BYTE "Enter the three exam grades: ",0
  19.     exam_prefix_s   BYTE "Exam",0
  20.     exam_post_s BYTE ": ",0
  21.  
  22.     total_s     BYTE "Total Grade = ",0
  23.     letter_s    BYTE "Letter Grade = ",0
  24.  
  25. .code
  26.  
  27. main PROC
  28.     call lab_in
  29.     call hw_in
  30.     call exam_in
  31.  
  32.     mov esi, OFFSET lab_grades  ; Pass lab_grades to proc.
  33.     call calc_dropped_total     ; EAX now has lab total, with lower grade
  34.                     ; dropped.
  35.  
  36.     mov edx, 5 
  37.     mul edx             ; EAX = EAX*5
  38.                     ; I couldn't find a way to eliminate
  39.                     ; the div later, so I figured I may
  40.                     ; as well use mul as well.
  41.  
  42.     mov ebx, eax            ; EBX = EAX*5
  43.  
  44.     mov esi, OFFSET hw_grades   ; Get ready to sum homeworks
  45.     call calc_dropped_total     ; EAX now has homeworks
  46.    
  47.     mov edx, 2          ; Yes, it's probably slower than adding
  48.     mul edx             ; it twice, but I want to be consistantA   
  49.     add ebx, eax            ; EBX = LT * 5 + HT * 2
  50.  
  51.     mov esi, OFFSET exam_grades ; Now we have to do the exams; no
  52.                     ; dropping required, so we'll do it
  53.                     ; here.
  54.    
  55.     movzx eax, BYTE PTR [esi]   ; EAX = E1
  56.     mov edx, 15
  57.     mul edx             ; EAX = E1 * 20
  58.     add ebx, eax            ; EBX = LT*5+HT*2+E1*15
  59.  
  60.     inc esi             ; Next one!
  61.     movzx eax, BYTE PTR [esi]   ; EAX = E2
  62.     mov edx, 20
  63.     mul edx             ; EAX = E2 * 20
  64.     add ebx, eax            ; EBX=LT*5+HT*2+E1*15+E2*20
  65.  
  66.     inc esi             ; Last one!
  67.     movzx eax, BYTE PTR [esi]   ; EAX = EF
  68.     mov edx, 30        
  69.     mul edx             ; EAX = EF * 30
  70.     add ebx, eax            ; EBX=LT*5+HT*2+E1*15+E2*20+EF*30
  71.  
  72.     mov eax, ebx
  73.     mov dl, 100        
  74.     div dl              ; Divide the whole thing by 100.
  75.                     ; AL has our total, the remainder is in
  76.                     ; AH. I'm not going check AH to round
  77.                     ; right because the example program
  78.                     ; doesn't.
  79.    
  80.     movzx eax, al           ; EAX now has our grade. We blank out
  81.                     ; the upper bits with movzx because
  82.                     ; WriteInt takes all of EAX.
  83.     ; Now EAX is our total!
  84.     mov edx, OFFSET total_s
  85.     call WriteString
  86.     call WriteInt
  87.  
  88.     call Crlf           ; A newline between them.
  89.  
  90.     call lookup_letter      ; Swap the grade in AL for an ASCII
  91.                     ; value of the grade.
  92.     mov edx, OFFSET letter_s
  93.     call WriteString
  94.     call WriteChar          ; Letter grade in Al.
  95.  
  96.     exit                ; Finallt done!
  97. main ENDP
  98.  
  99. lookup_letter PROC
  100.     ; Takes a grade in EAX and returns an ASCII value in al. We don't have
  101.     ; to ZX it because WriteChar just reads AL.
  102.  
  103.     cmp al, 85  ; Our value is 100 or less, so it fits into AL.
  104.     jg ltr_a
  105.    
  106.     cmp al, 75
  107.     jg ltr_b
  108.    
  109.     cmp al, 65
  110.     jg ltr_c
  111.    
  112.     cmp al, 59
  113.     jg ltr_d
  114.    
  115.     ; If we're here, the student failed. :(
  116.     mov al, 'F'
  117.     ret ; Return now al is set.
  118.  
  119. ltr_a:  mov al, 'A'
  120.     ret ; Return now al is set.
  121.  
  122. ltr_b:  mov al, 'B'
  123.     ret ; Return now al is set.
  124.  
  125. ltr_c:  mov al, 'C'
  126.     ret ; Return now al is set.
  127.  
  128. ltr_d:  mov al, 'D'
  129.     ret ; Return now al is set.
  130. lookup_letter ENDP
  131.  
  132. calc_dropped_total PROC ; This takes ESI (ptr to data) and
  133.             ; leaves EAX (total with drop).
  134.     ; WARNING! This trashes everything but ebx and edx
  135.     ; We have to add up the top scores while dropping the lowest.
  136.     ; I'll use an extra register and store the lowest while summing
  137.     ; then subtract it out afterwards.
  138.     push ebx    ; Store B and D.
  139.     push edx
  140.             ; Takes ESI.
  141.     mov bl, [esi]   ; Take the first grade and
  142.             ; put it in bl; we'll use B and D to compare scores.
  143.             ; I'll leave the lowest in D and compare the current (B)
  144.             ; against it.
  145.     movzx ax, bl    ; The l registers are going to top out! Need more space
  146.             ; for our running total, so we'll upgrade to x.
  147.    
  148.     inc esi     ; Next grade.
  149.     mov ecx,5   ; Five more to go.
  150.  
  151. add_l:  movzx dx, BYTE PTR [esi]    ; dl has new grade, eax is total
  152.                     ; bx is first value
  153.     add ax, dx  ; add to running total
  154.     cmp dl, bl  ; We don't have to ZX bl to bx because we can still
  155.             ; compare to the low half of bx with bl
  156.     jg skip     ; new grade is bigger, toss it!
  157.     mov bl, dl  ; But if new grade is smaller, save it in bl!
  158. skip:   inc esi     ; Get ready to test the next value
  159.     loop add_l  ; And do it again.
  160.  
  161.     ; At this point, the total is in AX, and the lowest value is in BL.
  162.     ; To subtract it, we have to ZX it to blank out the upper bits just in
  163.     ; case.
  164.     movzx bx, bl
  165.     sub ax, bx
  166.  
  167.     movzx eax, ax   ; WriteInt writes ALL of EAX, so blank out the rest
  168.             ; of it.
  169.  
  170.     pop edx     ; Restore D and B.
  171.     pop ebx
  172.  
  173.     ret
  174. calc_dropped_total ENDP
  175.  
  176. lab_in PROC ; Read in Lab grades
  177.     ; We're gonna need a lot of registers
  178.     push eax
  179.     push esi
  180.     push ecx
  181.     push edi
  182.     push edx
  183.  
  184.     mov edx, OFFSET lab_intro
  185.     call WriteString
  186.     call crlf
  187.  
  188.     mov esi, 0  ; Track number of lab we're on. (Zero indexed)
  189.  
  190.     mov edi, OFFSET lab_grades  ; Array to store our values
  191.     mov ecx, 6  ; 6 grades to get
  192.  
  193. read_l: mov edx, OFFSET lab_prefix_s    ; Start of prompt line "Lab"
  194.     call WriteString        ; Write it.
  195.  
  196.     mov eax, esi        ; Move number of lab we're on to eax
  197.     add al, 49      ; dec(48) == ascii('0'), dec(49) == ascii('1'), etc.
  198.                 ; Zero indexed, so add 1!
  199.     call WriteChar      ; Write lab number, no newline!
  200.  
  201.     mov edx, OFFSET lab_post_s  ; Get ready to write colon and space
  202.     call WriteString    ; Write them!
  203.  
  204.     call Readint        ; Read grade into al
  205.  
  206.     mov [edi], al       ; Store it in our array
  207.  
  208.     inc edi         ; Next byte please!
  209.     inc esi         ; Next number for prompt too!
  210.  
  211.     loop read_l     ; Read next score
  212.  
  213.     ; restore registers just in case
  214.     pop edx
  215.     pop edi
  216.     pop ecx
  217.     pop esi
  218.     pop eax
  219.  
  220.     ret
  221. lab_in ENDP
  222.  
  223. hw_in PROC ; Read in homework grades
  224.     ; We're gonna need a lot of registers
  225.     push eax
  226.     push esi
  227.     push ecx
  228.     push edi
  229.     push edx
  230.  
  231.     mov edx, OFFSET hw_intro
  232.     call WriteString
  233.     call crlf
  234.  
  235.     mov esi, 0  ; Track number of lab we're on. (Zero indexed)
  236.  
  237.     mov edi, OFFSET hw_grades   ; Array to store our values
  238.     mov ecx, 6  ; 6 grades to get
  239.  
  240. read_w: mov edx, OFFSET hw_prefix_s ; Start of prompt line "Hw"
  241.     call WriteString        ; Write it.
  242.  
  243.     mov eax, esi        ; Move number of lab we're on to eax
  244.     add al, 49      ; dec(48) == ascii('0'), dec(49) == ascii('1'), etc.
  245.                 ; Zero indexed, so add 1!
  246.     call WriteChar      ; Write lab number, no newline!
  247.  
  248.     mov edx, OFFSET hw_post_s   ; Get ready to write colon and space
  249.     call WriteString    ; Write them!
  250.  
  251.     call Readint        ; Read grade into al
  252.  
  253.     mov [edi], al       ; Store it in our array
  254.  
  255.     inc edi         ; Next byte please!
  256.     inc esi         ; Next number for prompt too!
  257.  
  258.     loop read_w     ; Read next score
  259.  
  260.     ; restore registers just in case
  261.     pop edx
  262.     pop edi
  263.     pop ecx
  264.     pop esi
  265.     pop eax
  266.  
  267.     ret
  268. hw_in ENDP
  269.  
  270. exam_in PROC ; Read in exam grades
  271.     ; We're gonna need a lot of registers
  272.     push eax
  273.     push esi
  274.     push ecx
  275.     push edi
  276.     push edx
  277.  
  278.     mov edx, OFFSET exam_intro
  279.     call WriteString
  280.     call crlf
  281.  
  282.     mov esi, 0  ; Track number of exam we're on. (Zero indexed)
  283.  
  284.     mov edi, OFFSET exam_grades ; Array to store our values
  285.     mov ecx, 3  ; 3 grades to get
  286.  
  287. read_e: mov edx, OFFSET exam_prefix_s   ; Start of prompt line "Exam"
  288.     call WriteString        ; Write it.
  289.  
  290.     mov eax, esi        ; Move number of lab we're on to eax
  291.     add al, 49      ; dec(48) == ascii('0'), dec(49) == ascii('1'), etc.
  292.                 ; Zero indexed, so add 1!
  293.     call WriteChar      ; Write lab number, no newline!
  294.  
  295.     mov edx, OFFSET exam_post_s ; Get ready to write colon and space
  296.     call WriteString    ; Write them!
  297.  
  298.     call Readint        ; Read grade into al
  299.  
  300.     mov [edi], al       ; Store it in our array
  301.  
  302.     inc edi         ; Next byte please!
  303.     inc esi         ; Next number for prompt too!
  304.  
  305.     loop read_e     ; Read next score
  306.  
  307.     ; restore registers just in case
  308.     pop edx
  309.     pop edi
  310.     pop ecx
  311.     pop esi
  312.     pop eax
  313.  
  314.     ret
  315. exam_in ENDP
  316.  
  317. END main
Add Comment
Please, Sign In to add comment