Advertisement
yugorin

Zadanie makra

May 2nd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //# Macros - Exercises
  2.  
  3. //#### 1. DWORD subtraction and comparison
  4.  
  5. //In the exercises below follow the "// TODO:" comments
  6.  
  7. //a) Create an assert_dwords_equal macro and test it
  8.  
  9. //b) Create a general subtract_integers macro and test if it forks for dwords
  10.  
  11. //c) Create an assert_dwords_not_equal macro and test it. To make this more challenging try to generalize it into the
  12. // assert_integers_not_equal macro.
  13.  
  14. :BasicUpstart2(main)
  15.  
  16. .const BORDER = $d020
  17.  
  18. .var brkFile = createFile("bin/breakpoints.txt")
  19.  
  20. .macro break() {
  21.   .eval brkFile.writeln("break " + toHexString(*))
  22. }
  23.  
  24. main:
  25.  
  26.  
  27. dword_equality_assertion_works:
  28.   :assert_dwords_equal(dword_a, dword_a)
  29.   :assert_dwords_equal(dword_b, dword_b)
  30.   :assert_dwords_equal(dword_c, dword_c)
  31.   :assert_dwords_equal(dword_d, dword_d)
  32.   :assert_dwords_equal(dword_e, dword_e)
  33.  
  34. // TODO: a) uncoment temporarily asserts below (one at the time) to check if the assert fails when it should
  35.   :break()
  36.   :assert_dwords_equal(dword_a, dword_b) // this should fail
  37. //  :assert_dwords_equal(dword_a, dword_c) // this should fail
  38. //  :assert_dwords_equal(dword_a, dword_d) // this should fail
  39. //  :assert_dwords_equal(dword_a, dword_e) // this should fail
  40.  
  41. dword_subtraction_works:
  42.   clc
  43.   :subtract_dwords(number_a, number_b, result)
  44. // TODO: b) uncomment asserts below if all previous tests are green
  45. //  :assert_dwords_equal(expected_result, result)
  46.  
  47.  
  48. dword_inequality_assertion_works:
  49. // TODO: c) uncomment asserts below if the tests are green
  50. // :assert_dwords_not_equal(dword_a, dword_b)
  51. // :assert_dwords_not_equal(dword_a, dword_c)
  52. // :assert_dwords_not_equal(dword_a, dword_d)
  53. // :assert_dwords_not_equal(dword_a, dword_e)
  54.  
  55. // TODO: c) uncoment temporarily asserts below (one at the time) to check if the assert fails when it should
  56. //  :assert_dwords_not_equal(dword_a, dword_a) // this should fail
  57. //  :assert_dwords_not_equal(dword_b, dword_b) // this should fail
  58. //  :assert_dwords_not_equal(dword_c, dword_c) // this should fail
  59. //  :assert_dwords_not_equal(dword_d, dword_d) // this should fail
  60. //  :assert_dwords_not_equal(dword_e, dword_e) // this should fail
  61.  
  62.  
  63.  
  64. render_test_result:
  65.   lda test_result
  66.   sta BORDER
  67.   rts
  68.  
  69. tests_fail:
  70.   lda #RED
  71.   sta test_result
  72.   jmp render_test_result
  73.  
  74. test_result:
  75.   .byte GREEN
  76.  
  77.  
  78. number_a:
  79.   .dword $11223344
  80.  
  81. number_b:
  82.   .dword $01020304
  83.  
  84. result:
  85.   .dword $0
  86.  
  87. expected_result:
  88.   .dword $11223344 - $01020304
  89.  
  90. dword_a:
  91.   .dword $11111111
  92. dword_b:
  93.   .dword $00111111
  94. dword_c:
  95.   .dword $11001111
  96. dword_d:
  97.   .dword $11110011
  98. dword_e:
  99.   .dword $11111100
  100.  
  101. .macro assert_dwords_equal(expected, actual) {
  102.     // TODO: a). implement this assertion
  103.     lda expected
  104.     cmp actual
  105.     jmp tests_fail
  106. }
  107.  
  108.  
  109. .macro subtract_dwords(a, b, result) {
  110.     :subtract_integers(32, a, b, result)
  111. }
  112.  
  113. .macro subtract_integers(bits, a, b, result) {
  114.     // TODO: b) implement this macro
  115. }
  116.  
  117. .macro assert_dwords_not_equal(expected, actual) {
  118.   // TODO: c) implement this macro
  119.   fail:
  120.     jmp tests_fail
  121.   pass:
  122. }
  123.  
  124. .macro assert_integers_equal(bits, expected, actual) {
  125.   .var bytes = bits_to_bytes(bits)
  126.  
  127.   .for(var byte = 0; byte < bytes; byte++) {
  128.     lda actual + byte
  129.     cmp expected + byte
  130.     bne fail
  131.   }
  132.     jmp pass
  133.   fail:
  134.     jmp tests_fail
  135.   pass:
  136. }
  137.  
  138. .function bits_to_bytes(bits) {
  139.   .return bits / 8
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement