User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Assembly section within the Software Development category of DaniWeb, a massive community of 427,177 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,160 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Assembly advertiser: Programming Forums
Aug 28th, 2006
Views: 4,644
This is a program I wrote for my x86 assembly class which computes the sum of n numbers. It uses Irvine32.inc which came with the textbook.
Last edited : Aug 28th, 2006.
asm Syntax
  1. title Number Addition (add.asm)
  2.  
  3. ; Dani Horowitz
  4. ; CSC111 x86 Assembly Programming
  5.  
  6. ; This program adds integers a user inputs
  7.  
  8. INCLUDE Irvine32.inc
  9.  
  10. ;--------------------------------------------------
  11. .stack ; begin stack segment
  12.  
  13. ;--------------------------------------------------
  14. .data ; begin data segment
  15.  
  16. sum dword 0
  17. counter dword 0
  18. prompt byte "Enter a value: (0 to stop) ", 0
  19. result byte "The sum of ", 0
  20. count byte " numbers is ", 0
  21.  
  22. ;--------------------------------------------------
  23. .code ; begin code segment
  24.  
  25. ;--------------------------------------------------
  26.  
  27.  
  28. ;--------------------------------------------------
  29. prompt_user PROC
  30. ;
  31. ; Take a break! Pauses screen
  32. ;--------------------------------------------------
  33.  
  34. mov edx, OFFSET prompt ; prompt user to enter values
  35. call WriteString
  36. call Crlf
  37.  
  38. GetValue:
  39.  
  40. call ReadInt ; read integer from keyboard
  41. jo Overflow ; if ReadInt set overflow flag, jump to Overflow
  42.  
  43. add sum, eax ; add entered value to accumulator sum
  44. jo Overflow ; if addition set overflow flag, jump to Overflow
  45.  
  46. cmp eax, 0 ; did user enter 0?
  47. je Done ; if yes, goto Done
  48.  
  49. inc counter ; increment counter
  50. jmp GetValue ; loop
  51.  
  52. Overflow:
  53.  
  54. mov eax, 0
  55. mov sum, 0
  56.  
  57. Done:
  58.  
  59. ret
  60. ;--------------------------------------------------
  61. prompt_user ENDP
  62. ;--------------------------------------------------
  63.  
  64. ;--------------------------------------------------
  65. print_sum PROC
  66. ;
  67. ; Take a break! Pauses screen
  68. ;--------------------------------------------------
  69.  
  70. mov edx, OFFSET result ; prints "The sum of "
  71. call WriteString
  72.  
  73. mov eax, counter ; prints count of #s entered
  74. call WriteDec
  75.  
  76. mov edx, OFFSET count ; prints " numbers is "
  77. call WriteString
  78.  
  79. mov eax, sum ; prints sum
  80. call WriteDec
  81. call Crlf
  82.  
  83. ret
  84. ;--------------------------------------------------
  85. print_sum ENDP
  86. ;--------------------------------------------------
  87.  
  88. ;--------------------------------------------------
  89. main proc
  90. ;--------------------------------------------------
  91. call Clrscr ; clear screen
  92. call prompt_user ; get integers
  93. call print_sum ; print their sum
  94. exit
  95.  
  96. main endp
  97. end main
  98. ;--------------------------------------------------
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 9:27 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC